Html 默认选中输入复选框

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

Input Checkbox checked by default

htmlcss

提问by Igorski88

For the life of me I cant set my CheckBox to checked at page load. I have tried adding value="true"and Checked="checked"after id and still nothing. Any ideas?

在我的一生中,我无法将 CheckBox 设置为在页面加载时检查。我试过 在 id 之后添加value="true"Checked="checked",但仍然没有。有任何想法吗?

<div class="onoffswitch-container">
<span class="onoffswitch-title">Open Inline</span>
<span class="onoffswitch">
<input type="checkbox" class="onoffswitch-checkbox" id="inline"> 
<label class="onoffswitch-label" for="inline">
<span class="onoffswitch-inner" data-swchoff-text="OFF" data-swchon-text="ON"></span>
<span class="onoffswitch-switch"></span>
</label>
 </span>
</div>

采纳答案by Igorski88

I tried all above solutions and nothing worked. It did give me some terminology to continue my research. What I ended up doing was going to the bottom of the code and added $('#inline').prop('checked', true);This solved my problem.

我尝试了上述所有解决方案,但没有任何效果。它确实给了我一些术语来继续我的研究。我最终做的是转到代码的底部并添加$('#inline').prop('checked', true);这解决了我的问题。

回答by Orange Orange

just write "checked" and it works

只需写“检查”就可以了

 <input type="checkbox" class="onoffswitch-checkbox" id="inline" checked> 

回答by Peter ID

added "true" parameter below, just try it before, and it works for me

在下面添加了“true”参数,之前尝试过,它对我有用

<input type="checkbox" class="onoffswitch-checkbox" id="inline" checked="true"> 

回答by Peter ID

The attribute checked="checked"does work and the shorthand notation is just checked.

该属性checked="checked"确实有效,并且简写符号只是checked.

<div class="onoffswitch-container">
<span class="onoffswitch-title">Open Inline</span>
<span class="onoffswitch">
<input type="checkbox" class="onoffswitch-checkbox" id="inline" checked="checked"/> 
<label class="onoffswitch-label" for="inline">
<span class="onoffswitch-inner" data-swchoff-text="OFF" data-swchon-text="ON"></span>
<span class="onoffswitch-switch"></span>
</label>
 </span>
</div>

回答by tao

You're looking for the checkedcontent attribute. Here's the relevant snippet from the official documentation: (Living Standard):

您正在寻找checked内容属性。这是官方文档中的相关片段:(生活标准):

The checked content attribute is a boolean attribute that gives the default checkedness of the input element. When the checked content attribute is added, if the control does not have dirty checkedness, the user agent must set the checkedness of the element to true; when the checked content attribute is removed, if the control does not have dirty checkedness, the user agent must set the checkedness of the element to false.

已检查的内容属性是一个布尔属性,它提供输入元素的默认检查性。添加checked content属性时,如果控件没有dirty checkness,则用户代理必须设置元素的checkness为true;当checked content属性被移除时,如果控件没有脏checkness,用户代理必须设置元素的checkness为false。

Note the docs have links to definition of relevant terms.

请注意,文档有指向相关术语定义的链接。

If it seems too technical, you can always go to MDN page on checked. MDN contributors have put up considerable effort into "translating" HTML Standard into more readable/understandable form (often times with examples), especially useful to people without a solid foundation on web related terminology and concepts.

如果它看起来太技术性,您可以随时访问MDN 页面checked。MDN 贡献者付出了相当大的努力将 HTML 标准“翻译”成更易读/易懂的形式(通常带有示例),这对于没有扎实的 Web 相关术语和概念基础的人尤其有用。

Note:You'll also find the direct answer to your questionon MDN.

注意:您还可以在 MDN 上找到问题的直接答案