我有一個數組初始化腳本指定選擇列表值陣列
var listarray = new Array();
而且具有動態創建的選擇列表---
<select multiple size=6 width=150 style="width:150px" name="ToLB" >
</select>
我的問題是如何分配的選擇所有值列表到數組。 在此先感謝。
我有一個數組初始化腳本指定選擇列表值陣列
var listarray = new Array();
而且具有動態創建的選擇列表---
<select multiple size=6 width=150 style="width:150px" name="ToLB" >
</select>
我的問題是如何分配的選擇所有值列表到數組。 在此先感謝。
你可以做到這一點像:
JQuery-
var optionVal = new Array();
$('select option').each(function() {
optionVal.push($(this).val());
});
Javascript成爲
var x = document.getElementById("mySelect");
var optionVal = new Array();
for (i = 0; i < x.length; i++) {
optionVal.push(x.options[i].text);
}
這家店在您的選擇框中的所有選項optionVal
。
希望它可以幫助你。
您可以使用getElementsByTagName
來獲取所有的selectbox
作爲對象。
var el = document.getElementsByTagName('select')
而在jQuery中,您可以按如下所示操作。
var arrayOfValues = $("select").map(function() { return this.value; });
看看http:// stackoverflow。com/questions/5866169 /正在獲取所有選擇的值 - 多選框 - 單擊按鈕-u以獲取更多信息 – 2013-04-23 07:55:24
使用普通的JavaScript這是一些適用於你的東西。現在 ,我認爲你在選擇
<select id="selecty" multiple size=6 width=150 style="width:150px" name="ToLB" >
<option value="monkey">Monkey</option>
</select>
var listarray = new Array();
//NOTE: Here you used new as a variable name. New is a reserved keyword so you cant use that as a variable name.
var select = document.getElementById('selecty'); #get the select list
for(var i = 0; i < select.options.length; i++){
listarray.push(select.options[i].value);
}
console.log(listarray);
>> ["monkey"]
小提琴這裏:
<html>
<body>
<!-- Any list (The main thing that it was built before the launch of the functions of the script) -->
<select id = "mySelect" multiple size=6 width=150 style="width:150px" name="ToLB" >
<option value="1111"></option>
<option value="12"> </option>
<option value="123"></option>
</select>
<script>
var valuesList = new Array();
var mySelect = document.getElementById("mySelect");
var currentOption = mySelect.childNodes;
for (var i=1; i<currentOption.length; i = i+2) { // i+2 needed in order to pass text nodes
valuesList[i-1]=currentOption[i].value;
}
</script>
</body>
</html>
這些值將存儲在您的數組中。我希望我能正確理解你。
你想要選擇的項目保存在數組中? – 2013-04-23 07:49:21
沒有列表的所有項目。 – NewBee 2013-04-23 07:53:37
我可以知道如何動態創建選擇列表?我想當生成選項時,你可以使用數組函數'push'來填充你的數組。 http://www.w3schools.com/jsref/jsref_push.asp – 2013-04-23 08:01:02