Home > Web Front-end > JS Tutorial > Why Doesn't My jQuery Mobile Checkbox Validation Work?

Why Doesn't My jQuery Mobile Checkbox Validation Work?

Susan Sarandon
Release: 2024-11-29 00:12:11
Original
292 people have browsed it

Why Doesn't My jQuery Mobile Checkbox Validation Work?

How to Determine if a Checkbox is Checked

In your mobile web application built with jQuery Mobile, you want to check the checked status of a checkbox. However, your code doesn't seem to be working.

You've tried using the following code:

<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

But it doesn't execute as expected.

The Solution

The reason for this behavior is that checked is a boolean property. Instead of comparing it to 1, you should use it directly in an if condition. Here's the corrected code:

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

This code correctly checks the checked status of the checkbox and displays the appropriate alert message.

The above is the detailed content of Why Doesn't My jQuery Mobile Checkbox Validation Work?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template