我想检查是否已使用 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">
我想检查是否已使用 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">
试试这个代码
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);
尝试
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');
}
演示:小提琴
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");
小提琴覆盖复选框和选择。
显然,这是一个非常冗长的示例,但您可以将其删减以删除您需要的内容。