2012-02-15 108 views
0

我有以下HTML。自动提交下拉选择表单*无*点击提交按钮? AJAX/PHP

目前它依靠用户点击'转到'按钮来提交表单。

是否有可能改变它,以便每次用户选择下拉选项时提交?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
    <title>Employees by Department</title> 
    <script src="ajax.js" type="text/javascript"></script> 
    <script src="dept.js" type="text/javascript"></script> 
    <style type="text/css" media="all">@import "style.css";</style> 
</head> 
<body> 
<!-- dept_form_ajax.html --> 
<p>Select a department and click 'GO' to see the employees in that department.</p> 
<form action="dept_results.php" method="get" id="dept_form"> 
<p> 
<select id="did" name="did"> 
<option value="1">Human Resources</option> 
<option value="2">Accounting</option> 
<option value="3">Marketing</option> 
<option value="4">Redundancy Department</option> 
</select> 
<input name="go" type="submit" value="GO" /> 
</p> 
</form> 

<select id="results"></select> 
</body> 
</html> 

为了记录在案,这里是我的dept.js文件内容:

// dept.js 

/* This page does all the magic for applying 
* Ajax to an employees listing form. 
* The department_id is sent to a PHP 
* script which will return data in HTML format. 
*/ 

// Have a function run after the page loads: 
window.onload = init; 

// Function that adds the Ajax layer: 
function init() { 

    // Get an XMLHttpRequest object: 
    var ajax = getXMLHttpRequestObject(); 

    // Attach the function call to the form submission, if supported: 
    if (ajax) { 

    // Check for DOM support: 
    if (document.getElementById('results')) { 

     // Add an onsubmit event handler to the form: 
     document.getElementById('dept_form').onsubmit = function() { 

     // Call the PHP script. 
     // Use the GET method. 
     // Pass the department_id in the URL. 

     // Get the department_id: 
     var did = document.getElementById('did').value; 

     // Open the connection: 
     ajax.open('get', 'dept_results_ajax.php?did=' + encodeURIComponent(did)); 

     // Function that handles the response: 
     ajax.onreadystatechange = function() { 
      // Pass it this request object: 
      handleResponse(ajax); 
     } 

     // Send the request: 
     ajax.send(null); 

     return false; // So form isn't submitted. 

     } // End of anonymous function. 

    } // End of DOM check. 

    } // End of ajax IF. 

} // End of init() function. 

// Function that handles the response from the PHP script: 
function handleResponse(ajax) { 

    // Check that the transaction is complete: 
    if (ajax.readyState == 4) { 

    // Check for a valid HTTP status code: 
    if ((ajax.status == 200) || (ajax.status == 304)) { 

     // Put the received response in the DOM: 
     var results = document.getElementById('results'); 
     results.innerHTML = ajax.responseText; 

     // Make the results box visible: 
     results.style.display = 'block'; 

    } else { // Bad status code, submit the form. 
     document.getElementById('dept_form').submit(); 
    } 

    } // End of readyState IF. 

} // End of handleResponse() function. 

这里任何指针非常感谢。

回答

3

这可能会给你的想法。

取代:

document.getElementById('dept_form').onsubmit = function() { 

与此:

$('#did').change(function() { 

或者说与此:

document.getElementById('did').onchange = function() { 
+0

认为这是我所追求的。你可以在编辑我的'dept.js'文件时解释我包含这个的位置吗?非常感谢 – michaelmcgurk 2012-02-15 06:46:12

+0

这没有奏效。我会看看JS错误控制台并报告回来。感谢您的帮助。 – michaelmcgurk 2012-02-15 06:50:34

+0

之前,我甚至从菜单中选择:'错误:失踪)参数列表 源文件:http://localhost/dept.js 行:50,列:4 源代码: } // DOM检查结束。 ' – michaelmcgurk 2012-02-15 06:53:31

1

你可以尝试选择使用:

<select id="did" name="did" onchange="this.form.submit();"> 
+0

唯一的问题与此是,它破坏我的AJAX功能。我希望我们能够继续使用AJAX。 – michaelmcgurk 2012-02-15 06:44:59