javascript - get and set selected value - Stack Overflow

REFRASE QUESTION :I have 6 selects. When I select value from select1 I invoke some function from server

REFRASE QUESTION :

I have 6 selects. When I select value from select1 I invoke some function from server side and I get JSON array from that function.

I get 5 values at most from this array, sometimes I'll get 20,30,40,50,60 but sometimes 20,30 or just 20.

These values correspond to select2, select3, select4, select5, select6 option value index. So in case the array returns 20,30,40,50,60 -> select2 option index value should be set to 20, select3 to 30 etc. And if array returns just 20 then select2 index value should be set to 20 and all others index values to 0.

What is the best way to do this?

Thank you

REFRASE QUESTION :

I have 6 selects. When I select value from select1 I invoke some function from server side and I get JSON array from that function.

I get 5 values at most from this array, sometimes I'll get 20,30,40,50,60 but sometimes 20,30 or just 20.

These values correspond to select2, select3, select4, select5, select6 option value index. So in case the array returns 20,30,40,50,60 -> select2 option index value should be set to 20, select3 to 30 etc. And if array returns just 20 then select2 index value should be set to 20 and all others index values to 0.

What is the best way to do this?

Thank you

Share Improve this question edited Dec 22, 2009 at 16:55 Gandalf StormCrow asked Dec 22, 2009 at 16:33 Gandalf StormCrowGandalf StormCrow 26.2k70 gold badges179 silver badges268 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2
var s1 = document.getElementById("select1");
var s2 = document.getElementById("select2");
s2.selectedIndex = s1.selectedIndex;

Or, if you want it to happen when the first <select> is changed:

s1.onchange = function() {
    s2.selectedIndex = s1.selectedIndex;
};
//get
var sel1Index = document.getElementById("select1").selectedIndex;
//just in case you want the selected text too
var sel1Text = document.getElementById("select1").options[selectedIndex].text; 

//set
document.getElementById("select2").selectedIndex = sel1Index;

To get a value, or the index of the selected value, these work:

document.getElementById("select1").value;
document.getElementByID("select2").selectedIndex;

To set a value, use the same, but assign:

document.getElementById("select1").value = "whatever";
document.getElementById("select2").selectedIndex = 3;

Because your list values are disimilar, I assume you want to use the index; ie, selecting "one" in select1 would cause select2 to change to "seven", etc.

You could do that using:

document.getElementById("select2").selectedIndex
  = document.getElementById("select1").selectedIndex;

If you want one list to change when the other changes, effectively keeping them in sync, you can bind functions to the select's "onchange":

<script type="text/javascript>
function select1_onchange() {
  document.getElementById("select2").selectedIndex
    = document.getElementById(select1").selectedIndex;
}

function select2_onchange() {
  document.getElementById("select1").selectedIndex
    = document.getElementById(select2").selectedIndex;
}

</script>

<select name="select1" id="select1" onchange="select1_onchange">
...


<select name="select2" id="select2" onchange="select2_onchange">
...

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742331310a4423775.html

相关推荐

  • javascript - get and set selected value - Stack Overflow

    REFRASE QUESTION :I have 6 selects. When I select value from select1 I invoke some function from server

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信