刷新后 HTML 复选框保持选中状态

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

HTML checkboxes keep checked after refresh

htmlcheckbox

提问by Dropout

I have a couple of check boxes in my markup.. For example:

我的标记中有几个复选框。例如:

<input type="checkbox" id="someCheckbox"/>

If I refresh the page with Ctrl+Shift+Reverything is OK - the page renders unchecked check boxes, however if some of them were checked and I refresh with F5they stay in their previous state.

如果我刷新页面Ctrl+Shift+R一切正常 - 页面呈现未选中的复选框,但是如果其中一些被选中并且我刷新F5它们保持在以前的状态。

Setting the checkedattribute doesn't work since having the attribute is enough to make it checked, the value is more or less irrelevant..

设置checked属性不起作用,因为拥有该属性足以使其检查,该值或多或少无关紧要..

How can I force them to be unchecked on page load, please?

请问如何强制在页面加载时取消选中它们?

回答by Dropout

I've found a solution which uses only HTML. If you add the autocomplete="off"attribute to the element it won't set the previous state after refresh..

我找到了一个只使用 HTML 的解决方案。如果您将autocomplete="off"属性添加到元素,它不会在刷新后设置以前的状态..

<input type="checkbox" id="foo" autocomplete="off"/>

回答by Bálint

You could set them back with javascript

您可以使用 javascript 将它们重新设置

var elements = document.getElementsByTagName("INPUT");
for (var inp of elements) {
    if (inp.type === "checkbox")
        inp.checked = false;
}

回答by salahuddin ansari

In hTML there is only checked attribute which force the checkbox to retain checked. and by default the check boxes are unchecked.If you want to force unchecked after load then don't use. One Important thingsome time it also depend on browser. for more detail referhttp://www.w3schools.com/html/html_form_input_types.asp

在 hTML 中,只有检查属性强制复选框保持选中状态。并且默认情况下复选框未选中。如果您想在加载后强制取消选中,请不要使用。 一件重要的事情有时也取决于浏览器。 有关更多详细信息,请参阅http://www.w3schools.com/html/html_form_input_types.asp