Get name of field being passed
I have two sets of lists (state lists) One called state_o and one called state_d and开发者_StackOverflow中文版 I have a function I do on them
function selectCountry(sel) {
document.getElementById("country_o").selectedIndex = states[sel.value];
}
Now what I want to do is determine if the "sel" is state_o or state_d and change the getElementById("country_o") to either _o or _d depending on what state is selected, so state_o would do country_o and state_d would do country_d
How can I determine the select field name?
Thanks!
You can access the name property through javascript:
function selectCountry(sel) {
var od = sel.name == "state_o" ? "o" : "d";
document.getElementById("country_"+od).selectedIndex = states[sel.value];
}
Or if you want to be a bit fancier and keep it on one line, slice the o/d straight from the name string:
function selectCountry(sel) {
document.getElementById("country_"+sel.name.slice(-1)).selectedIndex = states[sel.value];
}
If I've understood your question right, it's just:
var field;
if (sel.value == 'state_o')
field = 'country_o';
else
field = 'country_d';
document.getElementById(field).selectedIndex = states[sel.value];
If sel
is the select element, sel.id
or sel.name
should give you what you want.
Keep another parameter in this function to see if its _o or _d and set this value in calling function. then in your javascript method you can call country by -
document.getElementById("country" + param2)
Assuming param2 is that additional parameter and it will contain string "_o" or "_d"
Use sel.attr('name')
if you're using jQuery or sel.name
if just using plain old Javascript
Assuming you have:
<SELECT name='state_o'>
<OPTION value='blah'>BLAH</OPTION>
</SELECT>
<SELECT name='state_d'>
<OPTION value='blah'>BLAH</OPTION>
</SELECT>
精彩评论