获取选择框的选定值

我有选择框,并且选择的框选择了一个值,现在我可以更改值,我希望它应该选择更改的值而不是已经选择的值

我像这样使用它

var x= document.getElementById("numbervalue");
        var y= document.getElementById("dayvalue");
        ax = dnum.options[x.selectedIndex].value;
        at = dDay.options[y.selectedIndex].value;

上面只给我以前选择的值,而不是新的值


白衣染霜花
浏览 124回答 1
1回答

呼啦一阵风

希望这有帮助<!DOCTYPE HTML><html><head><meta charset="utf-8"><title>get value of select box</title><style>body {&nbsp; &nbsp; &nbsp; &nbsp; font-family: Courier, monospace;&nbsp; &nbsp; }#output {&nbsp; &nbsp; margin-top:10px;&nbsp; &nbsp; text-align:center;&nbsp; &nbsp; padding:10px;&nbsp; &nbsp; width: 200px;&nbsp; &nbsp; height:100px;&nbsp; &nbsp; border-style:groove;}.info {&nbsp; &nbsp; font-size: 12px;&nbsp;&nbsp; &nbsp; width: 270px;}</style></head><body><h3>select box stack overflow</h3><p class = "info">On any change in an option element, the JS will use the vals from the numbervalue option element to reset the val in the dayvalue option element.<br><br>You must change the number to change anything</p><label for = "dayvalue">Choose a day:</label><select name="dayvalue" id="dayVal" onchange="readBoxes()">&nbsp; &nbsp; <option value="m">Monday</option>&nbsp; &nbsp; <option value="t">Tuesday</option>&nbsp; &nbsp; <option value="w">Wednesday</option>&nbsp; &nbsp; <option value="th">Thursday</option>&nbsp; &nbsp; <option value="f">Friday</option>&nbsp; &nbsp; <option value="sat">Saturday</option>&nbsp; &nbsp; <option value="sun">Sunday</option></select><br><label for = "numbervalue">Choose a number:</label><select name="numbervalue" id="numVal" onchange="readBoxes()">&nbsp; &nbsp; <option value="1">1</option>&nbsp; &nbsp; <option value="2">2</option>&nbsp; &nbsp; <option value="3">3</option>&nbsp; &nbsp; <option value="4">4</option>&nbsp; &nbsp; <option value="5">5</option>&nbsp; &nbsp; <option value="6">6</option>&nbsp; &nbsp; <option value="7">7</option></select>&nbsp; &nbsp;&nbsp;<div id="output">Day of Week</div><p>-h34dsp1nns</p><script>function readBoxes() {&nbsp; &nbsp; var out = document.getElementById("output");&nbsp; &nbsp; var day = document.getElementById("dayVal");&nbsp; &nbsp; var num = document.getElementById("numVal");&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; var numIndex = num.selectedIndex;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; day.selectedIndex = numIndex; //resets the index&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; var dayVal = day[numIndex]; //gets the option element at an index&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; out.innerHTML = dayVal.text;}</script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript