简体   繁体   中英

JavaScript Code not working to check radio button

I am trying to do validation to check when a radio button is not selected, if it is not selected then a alert should be displayed, but the code below is not working. When the button is pressed thats when validation should happen

<html>
<head>
</head>

<body>
<form >
<input type="radio" id="b">
<input type="radio">

<button onClick="checkValidation()" type="button">Click Me!</button>
</form>
</body>


<script type="text/javascript">
 function checkValidation(){
 if (document.getElementById('b').checked == false) {
 window.alert("sometext");
  }
  </script>
</html>

missing } :

function checkValidation(){
 if (document.getElementById('b').checked == false) {
  window.alert("sometext");
 }
}

or remove { after the if :

function checkValidation(){
 if (document.getElementById('b').checked == false)
  window.alert("sometext");
}

Your code contains error, a missing closing semicolon, } , it should be:

function checkValidation(){
 if (document.getElementById('b').checked == false) {
 window.alert("sometext");
  }
} //This one is missing

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