简体   繁体   中英

How to validate if a checkbox is checked and if true generate code?

I am new to stackoverflow I usually do well finding my own answers, I'm not a javascript pro by no means, my question is probably a simple answer to some...lol

My example code looks at a text field with name input10 and then generates the code in the generator. For this I don't believe I need a text field; a checkbox would be much better and I would add value to the checkbox value="telephone=no"

What do I have to add to my code to work with a checkbox?

Example Code

if (form.input10.value != "") {
      form.generator.value +="<meta name=\"format-detection\" content=\"" +
      form.input10.value + "\">\n"; 
}

I humbly appreciate any positive feedback and help I get here at stackoverflow.

Thank you

<input id="tick" type="checkbox">

var tick = document.getElementById("tick");

function onChange(evt) {
    alert("Checkbox is " + (evt.target.checked ? "checked" : "unchecked"));
}

document.addEventListener("change", onChange, false);


console.log(tick.checked);

on jsfiddle

This is running on an event but checking the tickbox is just a matter of getting the value from tick.checked

As I said, I don't understand what your code is doing in the small amount of context that you have supplied but perhaps this is what you want, changing your textbox check to a checkbox check

if (form.tick.checked) {
      form.generator.value +="<meta name=\"format-detection\" content=\"" +
      form.input10.value + "\">\n"; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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