Javascript 在复选框上启用输入框

标签 javascript jquery html

我想当我选中我的复选框启用输入框时,我编写了这样的代码:

function free(){
    var free_var = document.getElementById('free_id');
    var delivery_price = document.getElementById('delivery_price');

    if(free_var.checked){
        delivery_price.disabled=true;
    }else{
        delivery_price.disabled=false;
    }                       
}   

但我的问题是我有更多这样的复选框和输入:

<input type="checkbox" name="site[]" value="47.5" class="c26"/> 37.5  <input type="text" name="quantity[]" placeholder="Količina" style="width:60px;" class="c26_i" disabled/>
    <input type="checkbox" name="site[]" value="48" class="c27"/> 41  <input type="text" name="quantity[]" placeholder="Količina" style="width:60px;" class="c27_i" disabled/>
    <input type="checkbox" name="site[]" value="48.5" class="c28"/> 44  <input type="text" name="quantity[]" placeholder="Količina" style="width:60px;" class="c28_i" disabled/>
    <input type="checkbox" name="site[]" value="49" class="c29"/> 47  <input type="text" name="quantity[]" placeholder="Količina" style="width:60px;" class="c29_i" disabled/>
    <input type="checkbox" name="site[]" value="49.5" class="c30"/> 50  <input type="text" name="quantity[]" placeholder="Količina" style="width:60px;" class="c30_i" disabled/>

这是 jsfiddle 中的代码:https://jsfiddle.net/b02e392v/

谢谢

最佳答案

$("input[type='checkbox']").change(function(){
    var status = this.checked;
    $(this).next().attr("disabled", !status);
});

checkbox 发生变化时获取是否被选中,相应地设置被点击的复选框旁边的 input 的 disabled 属性。

Play it here.

关于Javascript 在复选框上启用输入框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33787858/

相关文章:

c# - gridview 单元格内的 Javascript 验证

Jquery 平滑滚动偏移

javascript - 是否有一种很好的 AJAX/JQuery 验证表单的方法,可以很好地与 Django 的表单模型配合使用?

javascript - 如何使用 Jquery 在 AJAX 请求后替换元素的文本

html - css 表格列不会与固定标题对齐

javascript - 我的 API 调用没有返回任何内容?

javascript - JS下拉菜单被隐藏?或者不工作?

javascript - 在滚动条上翻转和旋转图像

javascript - 如何禁用jquery html5中文本框中的现有内容

if else 语句的 Jquery 性能