从带有mysql数据的列表框中获取选定的select值,以打开另一个列表框并使用该值获取mysql数据


Get selected select value from a listbox with mysql data to open another listbox and fetch mysql data using this value

我从javascript开始,所以我需要这样做:从一个列表框(已经有使用php的mysql数据)中,我想获得当前的选择id(select,我认为是在Java中选择的),并使用第一个列表框的id用数据填充列表框。但这必须发生在listbox1的onChange属性上,但必须发生在选择上。这是我目前所拥有的Html代码选择区域

$result_disp = mysql_query($query_disp, $cn);
$res=mysql_select_db("xxxx",$cn) or die("Note: " . mysql_error());
$res=mysql_query("select Region_ID, Region_Description from regions");
while($query_data = mysql_fetch_array($res))
{
?>
<option value="<? echo $query_data["Region_ID"]; ?>"
<?php if ($query_data["Region_ID"]==$_post['Region_ID']) ?>>
<? echo $query_data["Region_Description"]; ?></option>
<? } ?>
<select name="Dep" id="Items">
<option>Département</option>
</select>

javascript

function setSelect(id,value) {
var sel = document.getElementById(id);
var option, options = sel.options;
var i = options.length;
while (i--) {
option = options[i];
option.selected = (option.value == value)? true : false;
}
}

提前感谢!

如果您想操作和查询DOM,请使用很棒的javascript库jQuery。

一旦你下载了jQuery并将其包含在你的页面中,你就可以做这样的事情:

<select id='myFirstSelect'>
    <option value="1"/>
    <option value="2"/>
</select>

javascript:(将函数绑定到"changed"-事件)

$('#myFirstSelect').changed(function(){
    var selectedValue = $(this).val();
    //do something with selectedValue, e.g. $('#someOtherSelect').val(selectedValue);
});

我还没有测试过这段代码,它只是让你知道,一旦你进入jQuery(关键字:Selectors and Events)