简体   繁体   English

更改HTML复选框选中的属性

[英]Change HTML checkbox checked attribute

Is it possible to change the "checked" attribute of a checkbox? 是否可以更改复选框的“已检查”属性? So I want to change the preselect value in the HTML. 所以我想更改HTML中的预选值。 (So that I can get it later with innerHTML of JavaScript). (这样我以后可以用JavaScript的innerHTML来获取它)。

Sure, if you're going to use innerHTML in Javascript you could just reload the whole checkbox bit and make it checked or not. 当然,如果您要在Javascript中使用innerHTML,您可以重新加载整个复选框位并进行检查。 However the easier way to do it is to just change the checked attribute within Javascript if you're going to use JS anyway. 但是,更简单的方法是,如果您要使用JS,只需更改Javascript中的checked属性即可。

document.getElementById("myCheckboxId").checked = true;

EDIT 编辑

Ok, so you need to be able to get it again with innerHTML in the future. 好的,所以你需要能够在将来使用innerHTML再次获得它。 Like I said in my first sentence, you could just use innerHTML to change the checkbox as well. 就像我在第一句中说的那样,你也可以使用innerHTML来改变复选框。 May be a bit hackish but could be fine if you're doing it already. 可能有点hackish但如果你已经这样做可能会很好。

Here's a test html page with links to check/uncheck via JS, check/uncheck by swapping out the whole checkbox with innerHTML, and to get innerHTML for testing. 这是一个测试html页面,其中包含通过JS检查/取消选中的链接,通过使用innerHTML交换整个复选框来检查/取消选中,并获取innerHTML以进行测试。 When you do the innerHTML swap, it actually changes what's returned by innerHTML later. 当你进行innerHTML交换时,它实际上会改变innerHTML之后返回的内容。 Is this what you mean? 你是这个意思吗? Note: hmm I didn't realize it until now but it appears that firefox is changing my checked to checked="checked" , interesting. 注意:嗯,直到现在我才意识到它,但似乎firefox正在将我的检查更改为checked =“checked” ,这很有趣。 So the view innerHTML link shows me checked="checked" and when unchecked shows nothing. 所以视图innerHTML链接显示我检查=“已检查” ,未选中时显示任何内容。 Firebug shows it change also, but slightly differently - going from checked="" to nothing. Firebug也显示出它的变化,但略有不同 - 从checked =“”变为零。

Here is jsfiddle ! 这是jsfiddle

<span id="testdiv1"><input type="checkbox" name="heyhey" id="heyhey" CHECKED></span>
<p>
    <a href="#" onClick="document.getElementById('heyhey').checked = false;">uncheck via JS</a><br>
    <a href="#" onClick="document.getElementById('heyhey').checked = true;">check via JS</a><br>
    <a href="#" onClick="document.getElementById('testdiv1').innerHTML = '<input type=\'checkbox\' name=\'heyhey\' id=\'heyhey\' >';">uncheck via innerHTML swap</a><br>
    <a href="#" onClick="document.getElementById('testdiv1').innerHTML = '<input type=\'checkbox\' name=\'heyhey\' id=\'heyhey\' checked>';">check via innerHTML swap</a><br>
    <a href="#" onClick="alert('innerHTML!: '+'\n\n'+getElementById('testdiv1').innerHTML)">view innerHTML</a>
</p>

I don't know if I escaped all those properly but works for me. 我不知道我是否正常逃脱了所有这些但是对我有用。 Best to not do it inline. 最好不要内联。

Sounds like you have a legitimate use for setAttribute() rather than the equivalent property ( checked ). 听起来你有合法使用setAttribute()而不是等效属性(已checked )。 Assuming you have a checkbox stored in a variable called checkbox , you can use the following to set the checked attribute to be checked by default: 假设您有一个复选框存储在名为checkbox的变量中,您可以使用以下命令设置默认情况下要检查的checked属性:

checkbox.setAttribute("checked", "checked");

... and the following to remove the attribute and make the checkbox unchecked by default: ...以及以下内容删除属性并默认取消选中该复选框:

checkbox.removeAttribute("checked");

Note that this won't work properly on older IE, which has broken implementations of attribute-related methods. 请注意,这在旧的IE上无法正常工作,因为IE已经破坏了属性相关方法的实现。

I've written about this in more detail in an answer to previous question: Add attribute 'checked' on click jquery 我在回答上一个问题时更详细地写了这个: 在点击jquery上添加属性'checked'

对的,这是可能的

checkboxObject.checked=true|false

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

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