简体   繁体   English

从具有MySQL数据的列表框中获取选择的选择值,以打开另一个列表框并使用此值获取mysql数据

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

Im starting in javascript so, I need to do this : From a list box(already has mysql data using php) I want to get the current selection id (select,selected I think in Java) and fill a listbox with data using the id of the first listbox.But this must happens on onChange property of the listbox1 but on selection. 我从JavaScript开始,所以我需要这样做:从一个列表框(已经有使用PHP的MySQL数据)我想获取当前的选择ID(选择,我认为在Java中选择)并使用ID填充数据但这必须在listbox1的onChange属性上发生,但必须在选择时发生。 Here is what I have so far Html code Select a Region 到目前为止,这是我所拥有的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 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;

}
}

thanks in advance! 提前致谢!

If you want to manipulate and query the DOM, use the great javascript library jQuery . 如果您想操作和查询DOM,请使用强大的javascript库jQuery

Once you have jQuery downloaded and included in your page, you can then do things like this: 一旦下载了jQuery并将其包含在页面中,就可以执行以下操作:

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

javascript: (Bind a function to the 'changed'-event) javascript :(将函数绑定到“已更改”事件)

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

I haven't tested this code, it's just to give you an idea how much faster and more elegant you get ahead, once you get into jQuery (keywords: Selectors and Events) 我还没有测试过这段代码,只是想让您了解一下一旦使用jQuery(关键字:选择器和事件),您将获得更快,更优雅的体验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM