简体   繁体   中英

jQuery validate input type checkbox value

I'am stoked at a basic task. I want to check if a checkbox i checked or not using jQuery.

The problem is that the if condition is being executed no matter its checked or not.

What am I doing wrong?

($('#betingelser').prop('checked', false)){
    console.log("checked if");
    isValid = false;
    result += 'Venligst accepter betingelserne<br>';        
}

Thanks for your time

Thar

You are not having correct condition in if part. you are setting the value as false by using $('#betingelser').prop('checked', false) in it. To check whether checkbox is check use:

if($('#betingelser').is(':checked')){
   //rest code
}

By passing false as the second argument, you're actually telling the checkbox to check itself. Just delete this second argument and it should work fine.

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