Upon document load, am trying to trigger the click event of the first radio button.... but the click event is not triggered
.Also, tried 'change' instead of click ...but its the same result.
在文档加载时,我试图触发第一个单选按钮的单击事件....但是没有触发click事件。也尝试'更改'而不是单击...但是它的结果相同。
$(document).ready(function() {
//$("#checkbox_div input:radio").click(function() {
$("input:radio:first").prop("checked", true).trigger("click");
//});
$("#checkbox_div input:radio").click(function() {
alert("clicked");
});
});
Please follow the below link to the question
请按照以下链接查看问题
Example: http://jsbin.com/ezesaw/1/edit
示例:http://jsbin.com/ezesaw/1/edit
Please help me out in getting this right. Thanks!
请帮我解决这个问题。谢谢!
4 个解决方案
#1
32
You are triggering the event before the event is even bound.
您在事件被绑定之前触发事件。
Just move the triggering
of the event to after attaching the event.
只需在附加事件后将事件触发移动。
$(document).ready(function() {
$("#checkbox_div input:radio").click(function() {
alert("clicked");
});
$("input:radio:first").prop("checked", true).trigger("click");
});
检查小提琴
#2
11
Switch the order of the code: You're calling the click event before it is attached.
切换代码的顺序:您在连接之前调用click事件。
$(document).ready(function() {
$("#checkbox_div input:radio").click(function() {
alert("clicked");
});
$("input:radio:first").prop("checked", true).trigger("click");
});
#3
1
My solution is a bit different:
我的解决方案有点不同:
$( 'input[name="your_radio_input_name"]:radio:first' ).click();
#4
0
In my case i had to load images on radio button click, I just uses the regular onclick
event and it worked for me.
在我的情况下,我不得不在单选按钮上加载图像,我只是使用常规的onclick事件,它对我有用。
<input type="radio" name="colors" value="{{color.id}}" id="{{color.id}}-option" class="color_radion" onclick="return get_images(this, {{color.id}})">
<script>
function get_images(obj, color){
console.log($("input[type='radio'][name='colors']:checked").val());
}
</script>
#1
32
You are triggering the event before the event is even bound.
您在事件被绑定之前触发事件。
Just move the triggering
of the event to after attaching the event.
只需在附加事件后将事件触发移动。
$(document).ready(function() {
$("#checkbox_div input:radio").click(function() {
alert("clicked");
});
$("input:radio:first").prop("checked", true).trigger("click");
});
检查小提琴
#2
11
Switch the order of the code: You're calling the click event before it is attached.
切换代码的顺序:您在连接之前调用click事件。
$(document).ready(function() {
$("#checkbox_div input:radio").click(function() {
alert("clicked");
});
$("input:radio:first").prop("checked", true).trigger("click");
});
#3
1
My solution is a bit different:
我的解决方案有点不同:
$( 'input[name="your_radio_input_name"]:radio:first' ).click();
#4
0
In my case i had to load images on radio button click, I just uses the regular onclick
event and it worked for me.
在我的情况下,我不得不在单选按钮上加载图像,我只是使用常规的onclick事件,它对我有用。
<input type="radio" name="colors" value="{{color.id}}" id="{{color.id}}-option" class="color_radion" onclick="return get_images(this, {{color.id}})">
<script>
function get_images(obj, color){
console.log($("input[type='radio'][name='colors']:checked").val());
}
</script>