我尝试使用以下输出循环选择标记:
" 1 dropdown1,1 dropdown2,2 dropdown3,2 dropdown4,3 dropdown5,3 dropdown5 ......"
字符串或数组。
这是我到目前为止所做的,但我已经卡住了
var selects = document.getElementById("getAllRecordsDiv").getElementsByTagName("select");
for(var l = 0; l < selects.length; l++) {
for (var i = 0; i < 2; i++) {
var options = i + " " + selects[l].options[selects[l].selectedIndex].text;
}
}
<div id="getAllRecordsDiv" style="border: 2px solid gray; padding: 5px; display: none">
<select name="" id="dd1" style="" onchange="">
<option value="0">one</option>
<option value="0">two</option>
</select>
<select name="" id="dd2" style="" onchange="">
<option value="0">three</option>
<option value="0">four</option>
</select>
<select name="" id="dd3" style="" onchange="">
<option value="0">five</option>
<option value="0">six</option>
</select>
<select name="" id="dd5" style="" onchange="">
<option value="0">seven</option>
<option value="0">eight</option>
</select>
</div>
提前致谢。
答案 0 :(得分:0)
在你的最后解释之后,我明白你需要什么。这是:
var selects = document.getElementById("getAllRecordsDiv").getElementsByTagName("select");
var mainCount = countTo2 = 1;
var strOptions = '';
for (var l = 0; l < selects.length; l++) {
var options = selects[l].getElementsByTagName("option");
for (var i = 0; i < options.length; i++) {
var option = mainCount + " " + options[i].text + ", ";
strOptions += option;
countTo2++;
if (countTo2 > 2) {
countTo2 = 1;
mainCount++;
}
}
}
alert(strOptions);
最终(我相信)的例子在这里:
https://jsfiddle.net/53j3cgo2/6/
这是一个更高级的&#34;高级&#34;你可以研究一下这个例子:
function nodeListToArray(nodeList) {
return Array.prototype.slice.call(nodeList);
}
var selects = nodeListToArray(document.getElementById("getAllRecordsDiv").getElementsByTagName("select"));
var cnt = 1;
var options = '';
selects.forEach(function(select) {
nodeListToArray(select.getElementsByTagName("option"))
.forEach(function(option) {
options += ((cnt+(cnt % 2))/2) + " " + option.text + ", ";
cnt++;
});
});
alert(options);
它在这里:
https://jsfiddle.net/53j3cgo2/10/
可能有更好的方法来做到这一点。我邀请你学习更多有关JS对象,AMD和IIFE的信息吗? :)