Home  >  Article  >  Web Front-end  >  Why is my jQuery Mobile checkbox validation code failing?

Why is my jQuery Mobile checkbox validation code failing?

DDD
DDDOriginal
2024-11-17 04:52:03679browse

Why is my jQuery Mobile checkbox validation code failing?

How to Determine Checkbox Selection Status

Problem

You have written code in jQuery Mobile to validate if a checkbox is checked. Despite your efforts, the code fails to execute.

<script type="text/javascript">
  function validate(){
    if (remember.checked == 1){
      alert("checked") ;
    } else {
      alert("You didn't check it! Let me check it for you.")
    }
  }
</script>

<input>

Solution

The problem arises from incorrect comparison of the checked property. Instead of comparing it to 1, use the boolean property directly:

function validate(){
    var remember = document.getElementById('remember');
    if (remember.checked){
        alert("checked") ;
    }else{
        alert("You didn't check it! Let me check it for you.")
    }
}

The above is the detailed content of Why is my jQuery Mobile checkbox validation code failing?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn