[英]trouble setting “checked” property to checkbox jquery
I want to set all the checkbox inside a div
as checked when a particular radio button is selected. 我想在选中特定单选按钮时将
div
内的所有复选框设置为选中状态。 this is what i have tried but it didn't work. 这是我尝试过的,但是没有用。 I tried both
prop()
and attr()
. 我尝试了
prop()
和attr()
。
if($('#radio1').is(":checked",true)){
$('.checkbox-div').find('input[type=checkbox]').attr('checked','true');
}
<input type = "radio" id="radio1" name = "one">
<input type = "radio" id="radio2" name = "one">
<table class="table funds-table">
<thead class="table-head">
<tr>
<th id="equity-text">EQUITY</th>
<th id="eq-show-less">SHOW LESS</th>
</tr>
</thead>
<tbody id = "equity-body">
<tr>
<td class = "fund-name">
<div class = "col-xs-1 padding-lr-zero checkbox-div"><input type="checkbox" id = "checkbox1">
<label for="checkbox1"><span class="checkbox"><img src="icons/check_dis.png"></span></label></div>
<div class = "col-xs-11 fund-name-div"><span class = "span-fund-name">HDFC DYNAMIC FUND HDFC DYNAMIC FUND HDFC DYNAMIC</span><p class = "currently-inv"> Currently invested <span class = "curr-span-amt">Rs. 50,000</span></p></div>
</td>
<td class = "fund-amount"><p class = "fund-amount-inner">Rs. 50,00,000</p></td>
</tr>
<tr>
<td class = "fund-name">
<div class = "col-xs-1 padding-lr-zero checkbox-div"><input type="checkbox" id = "checkbox2">
<label for="checkbox2"><span class="checkbox"><img src="icons/check_dis.png"></span></label></div>
<div class = "col-xs-11 fund-name-div"><span class = "span-fund-name">HDFC DYNAMIC FUND HDFC DYNAMIC FUND HDFC DYNAMIC</span></div>
</td>
<td class = "fund-amount"><p class = "fund-amount-inner">Rs. 50,00,000</p></td>
</tr>
</tbody>
You are only checking if #radio1
is checked on page load. 您仅检查是否在页面加载时选中了
#radio1
。 If you want it to check whenever the user checks / unchecks #radio1
, use on()
. 如果希望用户每次检查/取消选中
#radio1
时都进行检查,请使用on()
。
Notice in the working example below, we bind to onchange
of both radios with the name of one
. 在下面的工作示例通知,我们绑定到
onchange
与名都收音机one
。 This preserves the checking / unchecking of the checkboxes when the user checks #radio2
: 当用户检查
#radio2
时,这将保留对复选框的检查/取消检查:
let $checkboxes = $('.checkbox-div').find('input[type=checkbox]'); $('input[name="one"]').on('change', function() { if( $(this).is(':checked') && $(this).attr('id') == 'radio1' ) { $checkboxes.prop('checked', true); } else { $checkboxes.prop('checked', false); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type = "radio" id="radio1" name = "one"> <input type = "radio" id="radio2" name = "one"> <table class="table funds-table"> <thead class="table-head"> <tr> <th id="equity-text">EQUITY</th> <th id="eq-show-less">SHOW LESS</th> </tr> </thead> <tbody id = "equity-body"> <tr> <td class = "fund-name"> <div class = "col-xs-1 padding-lr-zero checkbox-div"><input type="checkbox" id = "checkbox1"> <label for="checkbox1"><span class="checkbox"><img src="icons/check_dis.png"></span></label></div> <div class = "col-xs-11 fund-name-div"><span class = "span-fund-name">HDFC DYNAMIC FUND HDFC DYNAMIC FUND HDFC DYNAMIC</span><p class = "currently-inv"> Currently invested <span class = "curr-span-amt">Rs. 50,000</span></p></div> </td> <td class = "fund-amount"><p class = "fund-amount-inner">Rs. 50,00,000</p></td> </tr> <tr> <td class = "fund-name"> <div class = "col-xs-1 padding-lr-zero checkbox-div"><input type="checkbox" id = "checkbox2"> <label for="checkbox2"><span class="checkbox"><img src="icons/check_dis.png"></span></label></div> <div class = "col-xs-11 fund-name-div"><span class = "span-fund-name">HDFC DYNAMIC FUND HDFC DYNAMIC FUND HDFC DYNAMIC</span></div> </td> <td class = "fund-amount"><p class = "fund-amount-inner">Rs. 50,00,000</p></td> </tr> </tbody> </table>
You should try this. 您应该尝试一下。
$('#radio1').on('click',function(){
$('.checkbox-div').find('input[type="checkbox"]').attr('checked',$(this).prop('checked'));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.