如何触发单击 jquery 或 javascript 中选中的单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17978130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to trigger a click on checked radio button in jquery or javascript
提问by user2569524
Javascript:
Javascript:
if (GetCookie('prev_radio_value')!=null){
alert(GetCookie('prev_radio_value'));
$(":radio[value="+GetCookie('prev_radio_value')+"]").attr('checked',true);
$(":radio[value="+GetCookie('prev_radio_value')+"]").triggerHandler('click');
$(':radio:checked').triggerHandler('click');
$('input:radio[name=theme]:checked').click();
}else{
alert("clicking first");
$("input:radio:first").attr("checked", true).trigger("click");
}
HTML code:
HTML代码:
<ul>
<li><input type="radio" name="theme" value="theme1"/>Theme1</li>
<li><input type="radio" name="theme" value="theme2"/>Theme2</li>
</ul>
this code is inside div -> 'checkbox_div'
此代码位于 div -> 'checkbox_div'
click function :
点击功能:
$("#checkbox_div input:radio").click(function() {
alert("clicked") ;
});
I have used the trigger click in 3 ways but none of them worked or triggered the click event.
我以 3 种方式使用了触发器点击,但它们都没有工作或触发点击事件。
example link : http://jsbin.com/ezesaw/1/editis not triggering the click event upon selection of first radio button.
示例链接:http: //jsbin.com/ezesaw/1/edit在选择第一个单选按钮时不会触发点击事件。
回答by phiresky
Just call "click()" without the function as an argument
只需调用“click()”而不将该函数作为参数
$("#checkbox_div input:radio").click();
回答by user1721135
It works just fine:
它工作得很好:
http://jsbin.com/ivojoz/1/edit
http://jsbin.com/ivojoz/1/edit
The problem may be that you are not selecting the radio correctly
问题可能是您没有正确选择收音机
try
尝试
$('#checkbox_div input[type="radio"]').click(function() {
alert("clicked") ;
});
回答by Scaramouche
In case u are still interested in this question:
如果你仍然对这个问题感兴趣:
The click
event is implied when selecting/checking a radio button, all you need is to bind
a procedure to it, this is just ONE way:
click
选择/检查单选按钮时隐含该事件,您只需要对其bind
进行处理,这只是一种方式:
//js for radio button click
$('input[type="radio"]').on('click', function(){
window.alert($(this).val());
})
<!--html-->
<input type="radio" value="first" name="same"/>
<input type="radio" value="second" name="same"/>
<input type="radio" value="third" name="same"/>
回答by DominicValenciana
$("#checkbox_div").on('click','input:radio', function() {
alert("clicked");
});
Give this code a shot. It worked for me.
试试这个代码。它对我有用。
回答by Bad Loser
Whether done manually or programmatically, it seems that clicking an already checked radio button does not necessarilyhave any effect!
无论是手动完成还是以编程方式完成,似乎单击已选中的单选按钮不一定有任何效果!
Assuming HTML of:
假设 HTML 为:
<input type="radio" name="rv_submit" value="delete" id="rb2" checked>
Trigger the required action via jQuery as follows:
通过 jQuery 触发所需的操作,如下所示:
$('#rb2').prop('checked', false); // Turn off 'checked' ...
$('#rb2').click(); // ... or click() has no effect