Checkboxes
in HTML
forms don't have implicit labels with them. Adding an explicit label (some text) next to it doesn't toggle the checkbox
.
How do I make a checkbox toggle from clicking on the text label as well?
original title: "html - How do I make a checkbox toggle from clicking on the text label as well?"
Checkboxes
in HTML
forms don't have implicit labels with them. Adding an explicit label (some text) next to it doesn't toggle the checkbox
.
How do I make a checkbox toggle from clicking on the text label as well?
A HTML űrlapok jelölőnégyzetei nem tartalmaznak implicit címkéket. Egy explicit címke (valamilyen szöveg) hozzáadásával nem válthatja be a jelölőnégyzetet. Hogyan lehet bejelölni egy jelölőnégyzetet a képernyőn való kattintáshoz ...
Ez az összefoglalás a fordítás után. Ha meg szeretné tekinteni a teljes fordítást, kattintson a "fordítás" ikonra
Set the CSS
display
property for the label to be a block element and use that instead of your div - it keeps the semantic meaning of a label while allowing whatever styling you like.For example:
If you correctly markup your HTML code, there is no need for javascript. The following code will allow the user to click on the label text to tick the checkbox.
The for attribute on the label element links to the id attribute on the input element and the browser does the rest.
This has been testing to work in:
As indicated by @Gatekiller and others, the correct solution is the <label> tag.
Click-in-the-text is nice, but there is another reason to use the <label> tag: accessibility. The tools that visually-impaired people use to access the web need the <label>s to read-out the meaning of checkboxes and radio buttons. Without <label>s, they have to guess based on surrounding text, and they often get it wrong or have to give up.
It is very frustrating to be faced with a form that reads "Please select your shipping method, radio-button1, radio-button2, radio-button3".
Note that web accessibility is a complex topic; <label>s are a necessary step but they are not enough to guarantee accessibility or compliance with government regulations where it applies.
Ronnie,
If you wanted to enclose the label text and checkbox inside a wrapper element, you could do the following:
You can wrap your checkbox in the label:
You need to just wrap the checkbox in label tag just like this
FIDDLE
or you can also use the for attribute of label and id of your checkbox like below
FIDDLE
this should work:
if it doesnt, pleae corect me!
Wrapping with the label still doesn't allow clicking 'anywhere in the box' - still just on the text! This does the job for me:
but unfortunately has lots of javascript that is effectively toggling twice.
Hasonló problémák
Legutóbbi kérdések