JS:如果select是默认值,则禁用复选框
JS: Make checkboxes disabled if select is default value?
我有一组来自的复选框
<input type="checkbox" name="parts_hoses" id="parts-cb1" value="1">
通过id="parts-cb6"
我有一个#send-product
的选择框
<select name="send_product" id="send-product">
<option value="wall-mounted" selected>Wall-mounted (Default)</option>
<option value="freestanding">Freestanding</option>
<option value="third_party">Third Party</option>
</select>
当它处于默认值"壁挂式"时,复选框会被启用(默认情况下是这样),但当我将其切换到列表中的另一个选项时。。。我想禁用复选框。
这是我目前为止的JS(不起作用):
function switchProduct() {
var checkBoxes = document.querySelectorAll('input[type="checkbox"][id^="parts-cb"]');
var selectBox = document.getElementById('send-product');
if (selectBox.value == 'wall-mounted') {
checkBoxes.disabled = false;
} else {
checkBoxes.disabled = true;
}
}
document.getElementById('send-product').addEventListener('change', switchProduct);
我做错了什么?感谢您的帮助!
这是一把小提琴:https://jsfiddle.net/cwkgsuq1/
您缺少循环checkboxes
数组集合。
普通JS不是jQuery,因此"checkBoxes.disabled = false;
"将不起作用
改为:
for(var i=0; i<checkBoxes.length; i++) {
checkBoxes[i].disabled = false;
}
因此,简化后的代码可能看起来像:
function switchProduct() {
var checkBoxes = document.querySelectorAll('input[type="checkbox"][id^="parts-cb"]');
var selectBox = document.getElementById('send-product');
for(var i=0; i<checkBoxes.length; i++) {
checkBoxes[i].disabled = selectBox.value == 'wall-mounted';
}
}
document.getElementById('send-product').addEventListener('change', switchProduct);
switchProduct();
<select name="send_product" id="send-product">
<option value="wall-mounted" selected>Wall-mounted (Default)</option>
<option value="freestanding">Freestanding</option>
<option value="third_party">Third Party</option>
</select><br><br>
<input type="checkbox" id="parts-cb1" value="1">
<br>
<input type="checkbox" id="parts-cb2" value="1">
<br>
<input type="checkbox" id="parts-cb3" value="1">
相关文章:
- 检查数据库中复选框的值
- 以编程方式添加的复选框的值
- 通过原型/构造函数从复选框输出值
- 获取文本框中所有选定复选框的值
- 如何对选定的复选框行值求和
- 通过 JQuery 获取单击的复选框的值属性
- 使用 onChange 属性以内联方式更改复选框的值
- 获取 ajax 创建的复选框的值
- 获取 JavaScript 中复选框的值
- 普通获取选中复选框的值
- 如何获取切换复选框的值
- 根据MVC列表框和复选框的值更改标记元素
- 使用 Javascript 提取复选框的值
- 如何提交未选中复选框的值
- <JSP>正在获取复选框的值
- “角度表单”复选框默认值
- 复选框默认值“选中”,使用 jquery 1.9.1 给出未定义的值
- 使用Jquery给复选框默认的复选值
- 在rails上设置复选框默认值为true
- 如何按“名称”设置复选框默认值