我有 2 个下拉菜单,当我更改第一个下拉菜单时,第二个下拉菜单的值也会相应更改。 还有 2 个文本框,这 2 个下拉菜单各有 1 个。
当第一个下拉列表更改时,我运行 AJAX 并将 mySQL 中的值显示到第一个文本框中。正如我所说,第二个下拉列表的值也发生了变化。
我的问题是如何根据从第一个下拉列表自动更改的第二个下拉列表更改第二个文本框值?
这就是我更改第一个下拉列表时所调用的内容
function getVariety(seed_name) {
var strURL="findVariety.php?seed_name="+seed_name;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('seed_variety_show').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
findVariety.php
----mysql config variables------
.
.
<select class="form-control c-square c-theme input-lg" name="seed_variety">
<option value="">Επίλεξε</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value="<?=$row['sv_variety_name']?>"><?=$row['sv_variety_name']?></option>
<? } ?>
</select>
最佳答案
Use
value
property instead ofinnerHTML
.innerHTML
is used to push thehtml
content to the element whereas to set the value of theform
elements likeinput
/select
, usevalue
document.getElementById('seed_variety_show').value=req.responseText;
要更改第二个文本框的值,请在第二个select
上使用onchange
监听器。
由于选择是动态添加的,因此您需要使用事件委托(delegate)
。
试试这个:
$('body').on('change', '[name="seed_variety"]', function() {
alert(this.value);
});
关于javascript - 如何根据从第一个下拉列表自动更改的第二个下拉列表更改第二个文本框值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34671076/