【问题标题】:Unselect all checkboxes inside a specific div except the one checked取消选择特定 div 内的所有复选框,除了选中的复选框
【发布时间】:2026-02-21 21:05:01
【问题描述】:

这是我的 div

<div id="extras">
    <div class="col-md-12" id="extra-Material">
        <div class="form-group"><label>Material</label>
            <br
            ><label class="checkbox-inline mr10">
                <input name="ffp[]" type="checkbox" value="1_1"> Fabric</label>
            <label class="checkbox-inline mr10"><input name="ffp[]" type="checkbox" value="1_2"> Wood</label>
        </div>
    </div>
    <div class="col-md-12" id="extra-Color">
        <div class="form-group"><label>Color</label><br><label class="checkbox-inline mr10"><input name="ffp[]"
                                                                                                   type="checkbox"
                                                                                                   value="2_8">
                Brown</label><label class="checkbox-inline mr10"><input name="ffp[]" type="checkbox" value="2_5">
                Red</label></div>
    </div>
</div>

目前我有 2 个内部 div extra-Materialextra-Color。这些基本上是动态生成的。所有这些附加的 div 在其中都有相同的复选框名称ffp[]。我希望一次选择 1 个复选框。假设我单击“Fabric”,然后单击“Wood”,它应该只取消选择 Fabric(第一个 div 中 id = extra-Material 的所有复选框)应该取消选中,然后应该选中 Wood。与第二个 div 类似,依此类推。我已经搜索过,有代码可以取消选择页面上的所有复选框,但我不希望这样。谁能帮帮我?

这是我的 JS 代码

if (json_data.length > 0) {
    var html = '';
    html += '<div class="col-md-12" id="extra-' + name + '">'
    html += '<div class="form-group">';
    html += '<label>' + name + '</label>';
    html += '<br>';
    json_data.forEach(function (entry) {

        html += '<label class = "checkbox-inline mr10" >';
        html += '<input name="ffp[]" type = "checkbox" value = "' + val + '_' + entry.ffp_id + '"> ' + entry.en_name + '';
        html += '</label>';

    });
    html += '</div>';
    html += '</div>';
    $('#extras').append(html);
}

【问题讨论】:

  • 我会使用单选按钮作为材料(因为您一次只需要一个),然后单击单选按钮,从 extra-color$('input[type="checkbox"]').prop('checked',false) 中取消选中所有复选框

标签: javascript jquery html


【解决方案1】:

您必须将bind change 事件处理程序传递给input DOM 元素。

另外,使用.closest() 方法来获取当前集合中的所有输入元素。

您应该使用.on 方法来为动态添加的元素绑定事件处理程序。

事件委托允许我们将单个事件侦听器附加到父元素,该事件侦听器将为匹配选择器的所有后代触发,无论这些后代现在存在还是将来添加。

阅读有关事件委托的更多信息 here

$(document).on('change', 'input[type="checkbox"]', function(){
   $(this).closest('.col-md-12').find('input[type="checkbox"]').prop('checked',false);
   $(this).prop('checked',true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="extras">
    <div class="col-md-12" id="extra-Material">
        <div class="form-group"><label>Material</label>
            <br
            ><label class="checkbox-inline mr10">
                <input name="ffp[]" type="checkbox" value="1_1"> Fabric</label>
            <label class="checkbox-inline mr10"><input name="ffp[]" type="checkbox" value="1_2"> Wood</label>
        </div>
    </div>
    <div class="col-md-12" id="extra-Color">
        <div class="form-group"><label>Color</label><br><label class="checkbox-inline mr10"><input name="ffp[]"
                                                                                                   type="checkbox"
                                                                                                   value="2_8">
                Brown</label><label class="checkbox-inline mr10"><input name="ffp[]" type="checkbox" value="2_5">
                Red</label></div>
    </div>
</div>

【讨论】:

  • 完美。谢谢你。但是为什么之前的代码不起作用呢?
  • @AliZia,你应该使用on 方法。 $(document).on('change', 'input[type="checkbox"]', function(){ 用于动态添加的元素。
  • @AliZia,我更新了我的答案并为你解释。
【解决方案2】:

试试这个

$( "input[name='ffp[]']" ).bind( "change", function(){
    $(this).closest( ".form-group").find("input[name='ffp[]']").not($(this)).prop("checked", false);
});

演示

$( "#extras" ).on("change", "input[name='ffp[]']", function() {
  $(this).closest( ".form-group").find("input[name='ffp[]']").not($(this)).prop("checked", false);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="extras">
  <div class="col-md-12" id="extra-Material">
    <div class="form-group"><label>Material</label>
      <br><label class="checkbox-inline mr10"><input name="ffp[]" type="checkbox" value="1_1"> Fabric</label><label class="checkbox-inline mr10"><input name="ffp[]" type="checkbox" value="1_2"> Wood</label>
    </div>
  </div>
  <div class="col-md-12" id="extra-Color">
    <div class="form-group">
    <label>Color</label><br><label class="checkbox-inline mr10"><input name="ffp[]" type="checkbox" value="2_8"> Brown</label>
    <label class="checkbox-inline mr10"><input name="ffp[]" type="checkbox" value="2_5">Red</label>
    </div>
  </div>
</div>

【讨论】:

  • 它有什么作用?在将所有此类复选框附加到文档后,您需要绑定此事件。你可以分享一个小提琴吗?
  • @AliZia 我也分享了一个演示。
  • 这些 div 是在点击时创建的。也许这就是原因?
  • @AliZia 使用事件委托,我已经更新了代码
最近更新 更多