[英]Javascript: Passing data via an option element and “onchange”
I have a "select" element which is being populated by a PHP file. 我有一个由PHP文件填充的“选择”元素。 The output is a list of locations with names, addresses, cities, states, etc. I'd like to make it so when this dropdown is changed the new address is loaded into the address form.
输出是带有名称,地址,城市,州等的位置的列表。我想这样做,因此当更改此下拉列表时,会将新地址加载到地址表单中。
Here is the code I'd like to get working, which might explain this better: 这是我想开始使用的代码,可能会更好地解释这一点:
<select name='address' id='address'>
<option value='0' onselect='clearAddress()'></option>
<option value='1' onselect='loadAddress("1 Main St", "New York")'>Mark's House</option>
<option value='2' onselect='loadAddress("5 Arc Rd", "Orlando")'>Bob's Apt</option>
<option value='3' onselect='loadAddress("2 Fox Ln", "Clinton")'>Joe's Bar</option>
</select>
I know onselect doesn't work here, and while adding "onchange" to the select element might work, I can't see how it would function for individual options. 我知道onselect在这里不起作用,虽然在select元素中添加“ onchange”可能有用,但我看不到它对单个选项的作用。
Any thoughts on how to do this? 有关如何执行此操作的任何想法? Thanks!
谢谢!
May not be a proper solution but you can do it via 可能不是一个合适的解决方案,但是您可以通过
<select name='address' id='address' onchange="doCall(this)">
<option value='0' data-load=''></option>
<option value='1' data-load='1 Main St,New York'>Mark's House</option>
<option value='2' data-load='5 Arc Rd,Orlando'>Bob's Apt</option>
<option value='3' data-load='2 Fox Ln,Clinton'>Joe's Bar</option>
</select>
and 和
function doCall(elt) {
var params = elt.options[elt.selectedIndex].getAttribute("data-load").split(",");
alert(params[0]);
alert(params[1]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.