我希望检查我的多个下拉列表中是否有任何内容使用javascript选择。
<select name="id" id="id" size=22 multiple >
如果已选中任何复选框
<input type="checkbox" name="inst" class="asa" value="inst1">
<input type="checkbox" name="inst" class="asa" value="inst2">
答案 0 :(得分:1)
试试这段代码
<强> DEMO 强>
var selectVal = document.getElementById('id');
var selectCount = 0;
var values = [];
for (var i = 0; i < selectVal.options.length; i++) {
if (selectVal.options[i].selected) {
selectCount++;
values.push(selectVal.options[i].value);
}
}
表示复选框
<input type="checkbox" name="inst" class="asa" id="check1" value="inst1">
<input type="checkbox" name="inst" class="asa" id="check2" value="inst2">
var check1 = document.getElementById("check1").checked;
alert(check1);
var check2 = document.getElementById("check2").checked;
alert(check2);
答案 1 :(得分:0)
尝试
var select = document.getElementById('id');
var selected = [];
for(var i =0 ; i < select.options.length; i++){
if(select.options[i].selected){
selected.push(select.options[i].value);
}
}
if(selected.length == 0){
alert('not selected');
}
演示:Fiddle
答案 2 :(得分:0)
HTML:
<select name="id" id="three" size=22 multiple>
<option value="thevalue">Option</option>
</select>
<select name="id" id="four" size=22 multiple>
<option value="thevalue" selected="selected">Option</option>
</select>
JS:
var one = document.getElementById("one");
var two = document.getElementById("two");
var three = document.getElementById("three");
if(one.checked)
console.log("one = checked!");
else
console.log("one != checked");
if(two.checked)
console.log("two = checked!");
else
console.log("two != checked");
if(three.value)
console.log("something is selected in three!");
else
console.log("nothing is selected in three");
if(four.value)
console.log("something is selected in four!");
else
console.log("nothing is selected in four");
Fiddle覆盖复选框并选择。
显然这是一个非常详细的例子,但你可以将其修剪掉,以便带走你需要的东西。