简体   繁体   English

在页面加载时自动选中所有复选框

[英]Automatically have all checkboxes selected on page load

I can find a lot of code for a "select all" option, but what I want is more straightforward.我可以找到很多“全选”选项的代码,但我想要的更直接。 I want the WpForms checkboxes all checked by default on page load, not when a button is pressed.我希望 WpForms 复选框在页面加载时默认全部选中,而不是在按下按钮时选中。 I want them all selected so users only have to de-select the few they don't want.我希望它们都被选中,这样用户只需取消选择他们不想要的少数几个。

I am trying to use this code currently, please help.我目前正在尝试使用此代码,请帮忙。

$(':checkbox').each(function() {
    this.checked = true;                        
});

Thanks for any help.谢谢你的帮助。 I posted the above code into a javascript wordpress plugin but it didn't help.我将上面的代码发布到 javascript wordpress 插件中,但没有帮助。

on HTML just add checked as attribute for input type="checkbox"在 HTML 上,只需将checked添加为input type="checkbox"的属性

 <input type="checkbox" name="test1" value="test1" checked> I'm Check<br> <input type="checkbox" name="test2" value="test2" checked> I'm Check<br> <input type="checkbox" name="test3" value="test3"> I'm not Check<br> <input type="checkbox" name="test4" value="test4" checked> I'm Check

First of all my friend, you are not a retard;首先,我的朋友,你不是智障; You just need to gain more experience ;)您只需要获得更多经验;)

You should get checkboxes like this in Jquery and check them all:你应该在 Jquery 中得到这样的复选框并全部选中:

$('input[type=checkbox]').each(function () {
    this.checked = true;
});

This might help you.这可能对你有帮助。

About that wordpress plugin that you mentioned I have to say that you should put this piece of code in a script tag and a html file which has the checkBoxes in it.关于你提到的那个 wordpress 插件,我不得不说你应该把这段代码放在一个脚本标签和一个 html 文件中,里面有复选框。

you can use jquery for this solution您可以使用 jquery 解决此问题

$(document).ready || $(文档).ready || use for apply any function on load page用于在加载页面上应用任何 function

Solution:解决方案:

<input type="checkbox" name="test1" value="test1"> Test 1<br>
<input type="checkbox" name="test2" value="test2"> Test 2<br>
<input type="checkbox" name="test3" value="test3"> Test 3<br>
<input type="checkbox" name="test4" value="test4"> Test 4


$(document).ready(function(){
$('[type="checkbox"]').each(function(){
$(this).attr("checked","checked");
});
})

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

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