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

标签 jquery forms checkbox jquery-events

我有一个复杂的 jQuery 表单,如果选中某个复选框,我想禁用多个表单元素。我正在使用 jQuery 1.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>

这是我的 jQuery:

$('.element-toggle input').change(function () { 
    if ($(this).is(':checked')) $(this).parents('div.element-container').children('input,select').attr('disabled', true);
    else $(this).parents('div.element-container').children('input,select').removeAttr('disabled'); }); 

最佳答案

只是一个花絮:从jquery 1.6开始,你不应该使用 $(this).attr('checked') ,它总是正确的,而应该使用 $(this).prop('检查')

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

关于jQuery 在选中复选框时禁用表单元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1799255/

相关文章:

javascript - html中的表单点击提交按钮时无法提交数据?

php - 将单选框/复选框更改为对 PHP 编码进行最少更改的下拉框?

javascript - jQuery .data() 追加

jquery - jQuery ajax 成功回调后,CSS 可见性属性在 Webkit 浏览器上不起作用

php - 我的注册表详细信息未进入数据库(phpmyadmin)

forms - Symfony 2 - 如何删除日期字段类型的默认日期选择器?

jQuery noobie 无法使选中的复选框显示警报

c# - 带复选框的 ListView 获取选定的行索引

javascript - 在不丢失附加事件监听器的情况下修改 HTML 标记

javascript - event.stopPropagation() 在 firefox javascript 中不起作用