jQuery:在页面加载时触发单选按钮上的点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9922777/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:28:10  来源:igfitidea点击:

jQuery: Triggering a click event on a radio button on page load

jquery

提问by peipst9lker

I have 3 radio buttons which hide/show some div's. The radio buttons are getting generated by a php script, I want jquery to check the first radio button (works already!) and then trigger the click function so it hides/shows the related stuff.

我有 3 个单选按钮,它们隐藏/显示一些 div。单选按钮是由 php 脚本生成的,我希望 jquery 检查第一个单选按钮(已经工作!)然后触发点击功能,以便隐藏/显示相关内容。

$("input:radio:first").attr("checked", true).trigger("click");

The .trigger("click"); actually does not work.

.trigger("click"); 实际上不起作用。

The click event of the radio buttons looks like this:

单选按钮的点击事件如下所示:

$("input[name='type']").on("click", function() { ... });

Any ideas?

有任何想法吗?

回答by bArmageddon

Try doing it after DOM ready, like this:

尝试在 DOM 准备好后执行此操作,如下所示:

$(function(){
  $("input:radio:first").click();
});

Maybe it hasn't finish loading. use .click.

可能还没加载完。使用.click.