返回动态选择为null onChange

时间:2011-07-27 03:35:12

标签: javascript forms

我有两个select个框,第二个框取决于第一个框中的选择。我希望第一个框(course)更改时,第二个选择框(null)的值为subject。任何帮助实现这一点将不胜感激。

这就是我的尝试:

使用Javascript:

function autoSubmit() {
    var formObject = document.forms['theForm'];
    formObject.submit();
}

PHP:

<form name="theForm" method="get">
<select name="subject" onChange="autoSubmit();">
<option value="null">Select a Subject...</option>

<php
$sql = "SELECT DISTINCT subj_name, subj_id FROM table1 ORDER BY subj_name
$result = mysql_query($sql) or die ("couldn't execute query");

while($row = mysql_fetch_array($result))
{
echo ("<option value=\"$row[subj_id]\" " . ($subject == $row["subj_id"] ? " 
  selected" : "") . ">$row[subj_name]</option>");        
}
?>          
</select>

<select name="course" onChange="autoSubmit();">        
<option value="null">All Courses</option>        
<php
if($subject != null && is_numeric($subject))
{

$sql = "SELECT DISTINCT subj_id, course_id, course_name FROM table1 
  WHERE subj_id = $subject ORDER BY course_name
$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
{
echo ("<option value=\"$row[course_id]\" " . ($course == $row["course_id"] ? 
  " selected" : "") . ">$row[course_number] - $row[course_name]</option>");        
}
}

?>
</select>
</form>

1 个答案:

答案 0 :(得分:2)

您可以在autoSubmit()

之前输入第一个选择的onChange
this.form.course.value='null';

this.form.course.selectedIndex=0;

这样,获取的值将为?subject=[the_subject]&course=null(然后$course == $row["course_id"]将为false)。

http://jsfiddle.net/HMT34/