简体   繁体   English

jQuery验证输入类型复选框的值

[英]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. 我想检查我是否已使用jQuery选中的复选框。

The problem is that the if condition is being executed no matter its checked or not. 问题是无论是否检查if条件都在执行。

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. 您通过在其中使用$('#betingelser').prop('checked', false)将值设置为false。 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. 通过将false作为第二个参数传递,实际上是在告诉复选框进行自我检查。 Just delete this second argument and it should work fine. 只需删除第二个参数,它就可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM