[英]Radio button management - when I choose one, it automatically selects the other jQuery
I have 4 radio buttons, I would like when I click one (or set it to automatically select on start) so that when I select DeliveryYesSingle
, it will automatically select deliveryYesSingleBoard
.我有 4 个单选按钮,我希望当我单击一个时(或在开始时将其设置为自动 select),这样当我 select
DeliveryYesSingle
时,它会自动 select deliveryYesSingleBoard
。 however this function doesn't work like in If choose "if selected"然而,这个 function 不像在 If choose "if selected" 中那样工作
jQuery: jQuery:
*
if(jQ('[id="DeliveryYesSingle"]').is(':checked')){
jQ('[id="deliveryYesSingleBoard"]').prop('checked', true);
};
if(jQ('[id="DeliveryNoSingle"]').is(':checked')){
jQ('[id="DeliveryNoSingleBoard"]').prop('checked', true);
};
HTML: HTML:
<div class="row gutters">
<div class="col col-2">
<div class="form-item form-checkboxes">
<input type="radio" id="DeliveryYesSingle" name="deliveryCredit" value="yes"/><label data-timtranslationlabel="" for="deliveryCredit_Yes" class="checkbox" >Delivery on credit</label>
</div>
</div>
<div class="col col-2">
<div class="form-item form-checkboxes">
<input type="radio" id="DeliveryNoSingle" name="deliveryCredit" value="no"/><label data-timtranslationlabel="" for="deliveryCredit_No" class="checkbox" >Advance payment to supplier</label>
</div>
</div>
</div>
<div class="section" id="DeliveryOnCreditBoard" >
<div class="row gutters">
<div class="col col-2">
<div class="form-item form-checkboxes">
<input type="radio" id="deliveryYesSingleBoard" name="deliverySingle" value="yes"/><label data-timtranslationlabel="" for="deliveryYesSingleBoard" class="checkbox" >Delivery on credit</label>
</div>
</div>
<div class="col col-2">
<div class="form-item form-checkboxes">
<input type="radio" id="DeliveryNoSingleBoard" name="deliverySingle" value="np"/><label data-timtranslationlabel="" for="DeliveryNoSingleBoard" class="checkbox" >Advance payment to supplier</label>
</div>
</div>
</div>
</div>
https://jsfiddle.net/Palucci92/w4djnhgb/3/ https://jsfiddle.net/Palucci92/w4djnhgb/3/
Those two if
statements will run immediately after the page loads, and only once.这两个
if
语句将在页面加载后立即运行,并且只会运行一次。 if
statements should not be used to check for user input, such as selecting a radio button. if
语句不应用于检查用户输入,例如选择单选按钮。 You'll need to register an event on the two radio buttons you want to check for user input on, for instance:您需要在要检查用户输入的两个单选按钮上注册一个事件,例如:
jQ('[id="DeliveryYesSingle"]').on('change', () => {
});
In this case, the change
event is used to detect when a radio button is checked (see https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event ).在这种情况下,
change
事件用于检测单选按钮何时被选中(参见https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event )。
And within the callback function block (inside the curly braces), you can then perform your .prop()
call to change the property of the other radio buttons.在回调 function 块中(在大括号内),您可以执行
.prop()
调用来更改其他单选按钮的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.