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

Why is my jQuery Mobile checkbox validation code failing?

DDD
Release: 2024-11-17 04:52:03
Original
741 people have browsed it

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>
Copy after login

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.")
    }
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template