简体   繁体   English

jQuery 复选框:从单击方法更改为其他方法

[英]jQuery checkbox: change from a click method to something else

I have a jQuery method (function?) on a checkbox that executes when the checkbox is clicked我在单击复选框时执行的复选框上有一个 jQuery 方法(功能?)

click.function() {
 // blah blah
} 

But now I want the checkbox to be checked by default...how do I make the function run automatically (without waiting for the checkbox to be clicked on?)但是现在我希望默认选中该复选框...如何使 function 自动运行(无需等待单击复选框?)

$('#checkboxId').click(function() {
    // blah blah
}).trigger('click');

IMHO, it is better to abstract the logic out to server-side instead of programmatically clicking the checkbox on the client.恕我直言,最好将逻辑抽象到服务器端,而不是以编程方式单击客户端上的复选框。 What I mean is, pre-select the checkbox server-side by writing out the attribute checked="true" or checked="checked" (depending on doctype) and then also perform the function on load.我的意思是,通过写出属性checked="true"checked="checked" (取决于文档类型)预先选择服务器端复选框,然后在加载时执行function。

What if user clicks on the checkbox before the JS kicks in?如果用户在 JS 启动之前点击复选框怎么办? You might also want to disable the checkbox until your JS is fully loaded.您可能还想禁用该复选框,直到您的 JS 完全加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM