javascript - Unable to get a checkbox to be checked (not a simple question!) - Stack Overflow

admin2025-04-19  2

Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything, and its a plex/convoluted environment that my code is in.

Okay - here goes. I'm adapting someone else's html/js/css code and trying to get it to work on my server (I've taken permission - please, no ments on that). The main html generates a overlaid form which has a checkbox which is unchecked that I need to mark as checked before presenting to the user.

The checkbox code is just:

<input type="checkbox" id="type" />
<label for="type" id="disable">Disable check</label>

I've tried changing the above to ALL of the following (AND doing Ctrl+F5 when trying it out on the browser):

<input type="checkbox" id="type" CHECKED/>
<input type="checkbox" id="type" checked>
<input type="checkbox" id="type" checked="checked"/>
<input type="checkbox" id="type" checked="checked" value="1"/>
<input type="checkbox" id="type" value="true" checked>
<input type="checkbox" id="type" name="tempname" checked="checked"/>
<input type="checkbox" id="type" checked=true/>
At the end of the page: <script type="text/javascript">document.getElementById("type").checked=true;</script>

The problem COULD be elsewhere - something somewhere COULD be setting it to a default value of unchecked, but (a) that's a bit unlikely, and (b) I did look at the acpanying js code but no luck on a preliminary search.

Thoughts? 'cause I'm all out... :(

Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything, and its a plex/convoluted environment that my code is in.

Okay - here goes. I'm adapting someone else's html/js/css code and trying to get it to work on my server (I've taken permission - please, no ments on that). The main html generates a overlaid form which has a checkbox which is unchecked that I need to mark as checked before presenting to the user.

The checkbox code is just:

<input type="checkbox" id="type" />
<label for="type" id="disable">Disable check</label>

I've tried changing the above to ALL of the following (AND doing Ctrl+F5 when trying it out on the browser):

<input type="checkbox" id="type" CHECKED/>
<input type="checkbox" id="type" checked>
<input type="checkbox" id="type" checked="checked"/>
<input type="checkbox" id="type" checked="checked" value="1"/>
<input type="checkbox" id="type" value="true" checked>
<input type="checkbox" id="type" name="tempname" checked="checked"/>
<input type="checkbox" id="type" checked=true/>
At the end of the page: <script type="text/javascript">document.getElementById("type").checked=true;</script>

The problem COULD be elsewhere - something somewhere COULD be setting it to a default value of unchecked, but (a) that's a bit unlikely, and (b) I did look at the acpanying js code but no luck on a preliminary search.

Thoughts? 'cause I'm all out... :(

Share Improve this question edited Apr 22, 2022 at 10:21 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 2, 2009 at 11:40 SteveSteve 1,8775 gold badges32 silver badges46 bronze badges 1
  • Do you have some script that triggers a click on the label, as that would also change the state of the checkbox – Mottie Commented Nov 2, 2009 at 12:41
Add a ment  | 

2 Answers 2

Reset to default 3

It's a fairly simple question, if only because checkboxes are fairly simple things :) If

<input type="checkbox" id="type" name="name" checked="checked"/>

doesn't work then I would strongly suggest taking a bigger dig through the javascript being loaded on the page.

Have you looked into the JavaScript to see if there is code that is marking the checkbox unchecked onclick?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745001652a279262.html

最新回复(0)