我想知道是否可以将<select>
中的选定值存储在JS数组中。
我最终需要做的是计算大约10个下拉列表中的最高6个值,我认为我可以通过使用JS Math.max()函数来完成。
非常感谢帮助。
以下是一些示例代码:
<? while($subjects = mysql_fetch_array($query)) { ?>
<select class="points">
<optgroup label="<?=$subjects['name']?>">
<option value="100">A1</option>
<option value="90">A2</option>
<option value="85">B1</option>
<option value="80">B2</option>
<option value="75">B3</option>
</optgroup>
</select>
<? } ?>
<script>....
答案 0 :(得分:1)
做这样的事情(JQuery):
var selected = new Array();
$('.points option:selected').each(function() {
selected.push($(this).val());
});
答案 1 :(得分:0)
var selects = [].slice.apply(document.getElementsByTagName('select'));
selects.forEach(function (elem, i){
var value = elem.options[elem.selectedIndex].value;
//Do something with this value. Push it to an array, perhaps.
});
这假设应包含页面上的所有select
。如果不是这种情况,那么您应该使用document.getElementsByClassName
或类似的,更合适的选择器。
答案 2 :(得分:-1)
试试这个:
var selectValues = [];
var selectNodeList = document.getElementsByClassName("points");
for (var i = 0; i < selectNodeList.length; i++) {
selectValues.push(selectNodeList[i].value)
}
// selectValues array now stores values of all <select> lists with class "points"