单击功能时取消选中其他单选按钮
On click function uncheck other radio buttons
我给出了代码
<ul id="payment-method-fields">
<li id="paypal">
<label>
<input checked="checked" id="order_payments_attributes__payment_method_id_5" name="order[payments_attributes][][payment_method_id]" type="radio" value="5">
Test Paypal
</label>
</li>
<li id="adyen">
<label>
<input id="order_payments_attributes__payment_method_id_4" name="order[payments_attributes][][payment_method_id]" type="radio" value="4">
Ali Pay
</label>
</li>
<li id="adyen_encrypted">
<label>
<input id="order_payments_attributes__payment_method_id_16" name="order[payments_attributes][][payment_method_id]" type="radio" value="16">
credit card
</label>
</li>
<li id="cod" >
<label>
<input id="cash-on-delivery" name="cod" type="radio" value="">
Cash on Delivery
</label>
</li>
</ul>
我的要求是当单击其他li时,即货到付款,然后PayPal被取消选中。请指导我将如何编写此jquery。
我的建议是:
$('#payment-method-fields :input[type="radio"]'(.on('change', function(e( { 如果 (this.id == '货到付款'( { $('#payment-method-fields'(.find('input[type="radio"]:not(#' + this.id + '('(.prop('checked', false(; }});
你可以这样做:
$('#cash-on-delivery').on('change', function(){
$('input[name="order[payments_attributes][][payment_method_id]"]')
.prop('checked', !this.checked);
});
只需为所有输入标签指定相同的名称即可例:
<input type="radio" name="group_name" />
这可确保在任何时候只选择一个。
例如,
您可以为所有输入提供相同的类
类="radio_button">
然后你可以添加代码
$(document).ready(function(){
$('.radio_button').on('click',function(){
$('.radio_button').removeAttr('checked');
$(this).prop('checked','checked');
})
}
最后一个input
的名称<input id="cash-on-delivery" name="cod" type="radio" value="">
与其他名称不同,因此当您选中它时,其他不会自动取消选中。给它相同的名称,它将正常工作。
这是它工作的示例。
法典:
<ul id="payment-method-fields">
<li id="paypal">
<label>
<input checked="checked" id="order_payments_attributes__payment_method_id_5" name="order[payments_attributes][][payment_method_id]" type="radio" value="5">
Test Paypal
</label>
</li>
<li id="adyen">
<label>
<input id="order_payments_attributes__payment_method_id_4" name="order[payments_attributes][][payment_method_id]" type="radio" value="4">
Ali Pay
</label>
</li>
<li id="adyen_encrypted">
<label>
<input id="order_payments_attributes__payment_method_id_16" name="order[payments_attributes][][payment_method_id]" type="radio" value="16">
credit card
</label>
</li>
<li id="cod" >
<label>
<input id="cash-on-delivery" name="order[payments_attributes][][payment_method_id]" type="radio" value="">
Cash on Delivery
</label>
</li>
</ul>
使用 jQuery removeProp 函数从输入元素中删除属性 'checked'。对于您的示例,创建两个函数并在输入元素的 onClick 触发器中调用它们:
function uncheckTop3() {
$('#order_payments_attributes__payment_method_id_5').removeProp('checked');
$('#order_payments_attributes__payment_method_id_4').removeProp('checked');
$('#order_payments_attributes__payment_method_id_16').removeProp('checked');
}
function uncheckCashOnDelivery() {
$('#cash-on-delivery').removeProp('checked');
}
这里有一个指向你的例子的jfiddle链接。
要选中/取消选中复选框,请使用选中的属性并对其进行更改。使用 jQuery,您可以执行以下操作:
//$('#myCheckbox').attr('checked', true); // Checks it
//$('#myCheckbox').attr('checked', false); // Unchecks it
$('#cash-on-delivery').on('change', function(){
$('input[name="order[payments_attributes][][payment_method_id]"]').attr('checked',true);
});
相关文章:
- 选择其中一个单选按钮,然后单击“提交”,重定向其他页面
- 其他单选按钮在使用 Javascript 函数根据 if 语句选择某些单选按钮后不可单击
- 单击其他单选按钮后取消选中单选按钮
- 如何在 Javascript 中禁用和启用其他按钮,例如单选按钮效果
- 如何在选择其他单选按钮后将其灰显,直到刷新
- 在选择任何其他单选按钮时清除文本框
- 如何一次只选择一个单选按钮,并在数据来自数据库时在javascript中取消选择其他单选按钮
- 单击功能时取消选中其他单选按钮
- 如何在jsp中插入单选按钮、复选框和其他输入类型
- JQuery移动单选按钮在选择其他按钮时保持选中状态.使用.html函数
- 谷歌地图-缩小时,信息窗口被关闭或其他单选按钮过滤器被“勾选”
- 使单选按钮单一选择:除了使用相同的名称之外的其他方式
- 操纵其他DOM元素时未选择单选按钮
- 如何使用Javascript在其他单选按钮中检查特定的单选按钮
- 如何使用选项为其他html元素的单选按钮
- 如何在未选中单选按钮时隐藏内容,并允许查看其他内容
- 禁用Asp.net单选按钮列表中的其他单选按钮(如果其中一个已选中)
- jquery代码工作正常,但其他单选按钮干扰了它的工作
- 将文本框“其他”值分配给单选按钮值
- JQuery .prop(checked,true)不将其他单选按钮设置为false