简体   繁体   中英

Linking HTML Checkbox to Javascript

How could I link a checkbox from HTML into java script ? I have built the checkbox in HMTL like so : <label><b>Response Needed ?</b></label> <input id="Check" type="checkbox" >

When I hit the submit button I need the value to be T (true) or F (false) if its checked or not..

I usually use getElementById("") to link things into Javascirpt but cant get this to work!

EDIT: Here is my full Document so you can see how its works :

`

<!DOCTYPE HTML>
<html>
<head lang="en">
<meta charset="UTF-8"> 

 </head>

<body>

<form id="OTA">

    <label><b>IMEI:</b></label>
<input type="text" name="ID" id="IMEI" maxlength="15"> 

    <label><b>AT Command:</b></label>
    <input id="Command" type="text">

    <label><b>Response Needed ?</b></label>
    <input id="Check" type="checkbox" >


</form>
    <input type="submit" value="Submit" onclick="showInput();"><br />
    <p><span id='display'></span> </p> 

    <script language="JavaScript">
function showInput() {

    var message_entered = ">RSP=" + document.getElementById("Check").checked + ";ID=" +  document.getElementById("IMEI").value + ";" + document.getElementById("Command").value + "<";

    document.getElementById('display').innerHTML = message_entered;

}
    </script>

</body>

</html>

I need to change the values of the checkbox to T and F when checked or not. Cheers

Well... if you want to check whether a checkbox is checked or not (True or False), you can use document.getElementById("Check").checked

Example

if (document.getElementById("Check").checked) 
    console.log("checked");
else 
    console.log("not checked");

here is some javascript

  <form id="OTA"> <label><b>IMEI:</b></label> <input type="text" name="ID" id="IMEI" maxlength="15"> <label><b>AT Command:</b></label> <input id="Command" type="text"> <label><b>Response Needed ?</b></label> <input id="Check" type="checkbox" > </form> <input type="submit" value="Submit" onclick="showInput();"><br /> <p><span id='display'></span> </p> <script language="JavaScript"> function showInput() { var test=null; var obj=document.getElementById("Check").checked; obj?test='t':test='f'; var message_entered = ">RSP=" +test+ document.getElementById("IMEI").value + ";" + document.getElementById("Command").value + "<"; document.getElementById('display').innerHTML = message_entered; } </script> 

On your submit button call some javascript method like below:

<form action="someAction" method="post"  onSubmit="javascript:validate()">
<input type="hidden" name="checkValue" id="checkValue" value='F'/> <!--Default value is F say -->
<submit></submit>
</form>

Now, below is validate function:

<script>
function validate(){
  if (document.getElementById("Check").checked){ 
     document.getElementById("checkValue").value = 'T'
  }else{ 
     document.getElementById("checkValue").value = 'F'
  }
}
</script>

There is just one disadvantage that you need to have checkbox with name different than original name you wanted as that original name would go to hidden field.

Hope it helps!!

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