Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
选中复选框时,jQuery禁用表单元素_Jquery_Forms_Checkbox_Jquery Events - Fatal编程技术网

选中复选框时,jQuery禁用表单元素

选中复选框时,jQuery禁用表单元素,jquery,forms,checkbox,jquery-events,Jquery,Forms,Checkbox,Jquery Events,我有一个复杂的jQuery表单,如果选中某个复选框,我想禁用几个表单元素。我正在使用jQuery1.3.2和所有相关插件。我做错了什么 这是我的HTML: <li id="form-item-15" class="form-item"> <div class='element-container'> <select id="house_year_built"

我有一个复杂的jQuery表单,如果选中某个复选框,我想禁用几个表单元素。我正在使用jQuery1.3.2和所有相关插件。我做错了什么

这是我的HTML:

    <li id="form-item-15" class="form-item">
        <div class='element-container'>
            <select id="house_year_built" name="house_year_built" class="form-select" >
                 ...Bunch of options...
            </select>
            <input type="text" title="Original Purchase Price" id="house_purchase_price" name="house_purchase_price" class="form-text money" />
            <input type="text" title="Current Home Debt" id="house_current_debt" name="house_current_debt" class="form-text money" />
        </div>
        <span class="element-toggle">
            <input type="checkbox" id="house_toggle" />
            <span>Do not own house</span>
        </span>
    </li>

不是
$('.element-toggle-input')的父对象
是您的示例

有一些事情需要更改。首先,实际的HTML结构与您在JavaScript中查询的内容不匹配(特别是
parents()
调用)。其次,绑定到
click
事件将在IE中提供更好的支持,因为IE有时会等待触发
change
事件,直到元素失去焦点

$('.element-toggle input').bind('click', function () {
    var inputs = $(this).closest('li.form-item').find('div.element-container').children('input,select');

    if ($(this).attr('checked')) {
        inputs.attr('disabled', true);
    } else {
        inputs.removeAttr('disabled');
    }
});

这很有帮助。如果有人在寻找一个插件,你可以这样做:

只是一个小插曲:因为jquery 1.6,你不应该使用
$(this).attr('checked')
,这总是正确的,而是
$(this).prop('checked')


建议使用
$(this.prop('disabled')
(测试元素是否被禁用)和
$(this.prop('disabled',newState)
,而不是
attr

仍然不工作,但看起来应该工作。是否有可能bind()比onchange更脆弱,即使兼容性较差。我会继续玩下去。嗯,我最终消除了元素容器类,因为它不适合我,我使用:$('.element-toggle-input').bind('click',function(){var-inputs=$(this).closest('li.form-item').find('input,select');if($(this.attr('checked'))inputs.attr('disabled',true);else-inputs.removeAttr('disabled')$(this).removeAttr('disabled');});再次感谢,DWWorks可以工作,但成本为3美元或15美元,并且不是开源的。if语句也可以省略,从而从
inputs.prop('disabled',$(this).prop('checked');
$('.element-toggle input').bind('click', function () {
    var inputs = $(this).closest('li.form-item').find('div.element-container').children('input,select');

    if ($(this).attr('checked')) {
        inputs.attr('disabled', true);
    } else {
        inputs.removeAttr('disabled');
    }
});