[英]Using jQuery to trigger radio button onclick event
I have a code where I am selecting radio button using the .prop()
method. 我有一个使用
.prop()
方法选择单选按钮的代码。 Issue is I also have the onclick event on radio button is there any workaround to make it trigger an onclick event as well using jQuery 问题是我也有单选按钮上的onclick事件 ,是否有任何变通办法使它也可以使用jQuery触发onclick事件
jQuery Code jQuery代码
/**
* Select shipping method when selected from the list
*/
$(document).ready(function() {
var $options = $('.shipping-method .item');
$options.click(function(e) {
var $current = $(this);
e.preventDefault()
e.stopPropagation()
$options.removeClass('on')
$current.addClass('on')
$('input', $current).prop('checked', true)
})
})
HTML Code HTML代码
<li>
<div class="item"><a href="javascript:;" class="shipping_3">Home Delivery</a>
<input name="shipping" type="radio" class="radio" id="shipping_3" value="3" checked="checked" supportcod="1" insure="0" onclick="selectShipping(this)">
</div>
</li>
Here you go with a solution 在这里你有一个解决方案
$current.prop('checked', true).trigger("click"); $ current.prop('checked',true).trigger(“ click”);
$(document).ready(function() {
var $options = $('.shipping-method .item');
$options.click(function(e) {
var $current = $(this);
e.preventDefault()
e.stopPropagation()
$options.removeClass('on')
$current.addClass('on')
$current.prop('checked', true).trigger("click");
});
});
My suggestion would be to remove onclick attribute with a separate event listener first. 我的建议是先使用单独的事件侦听器删除onclick属性。
Refer jQuery.click() vs onClick . 参考jQuery.click()与onClick 。
Remove line: 删除行:
e.preventDefault()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.