2017-10-04 53 views
2

这是我的div取消选择,除了一个特定的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个复选框被选中。假设我点击'布料',然后点击'木材',它应该取消选中布料(第一个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); 
} 
+0

我会使用单选按钮材料(如你只需要一个在时间),并在单选按钮的点击,取消选择所有复选框从额外颜色'$('input [type =“checkbox”]')。prop('checked',false)' –

回答

1

您必须bind a change事件处理程序到inputDOM元素。

此外,使用.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>

+0

完美。谢谢。但为什么代码之前不工作? –

+0

@AliZia,你应该使用'on'方法。 '$(document).on('change','input [type =“checkbox”]',function(){'为动态添加的元素 –

+0

@AliZia,我更新了我的答案并给出了一个解释。 –

1

试试这个

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

DEMO

$("#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>

+0

它不适用于我 –

+0

它是做什么的?将所有这些复选框附加到文档后,您需要绑定此事件。你有可能分享一个小提琴吗? – gurvinder372

+0

@AliZia我也分享了一个演示。 – gurvinder372

相关问题