Javascript 测试是否使用 jQuery 选中复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4813219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:13:17  来源:igfitidea点击:

Testing if a checkbox is checked with jQuery

javascriptjquerycheckboxjquery-selectors

提问by Rajeev

If the checkbox is checked, then I only need to get the value as 1; otherwise, I need to get it as 0. How do I do this using jQuery?

如果复选框被选中,那么我只需要将值设为 1;否则,我需要将其设为 0。如何使用 jQuery 执行此操作?

$("#ans").val()will always give me one right in this case:

$("#ans").val()在这种情况下,我将永远给我一个权利:

<input type="checkbox" id="ans" value="1" />

回答by Andy Mikula

Use .is(':checked')to determine whether or not it's checked, and then set your value accordingly.

使用.is(':checked')以确定它是否已选中,然后设置相应的值。

More information here.

更多信息在这里。

回答by xaxxon

$("#ans").attr('checked') 

will tell you if it's checked. You can also use a second parameter true/false to check/uncheck the checkbox.

会告诉你它是否被检查。您还可以使用第二个参数 true/false 来选中/取消选中复选框。

$("#ans").attr('checked', true);

Per comment, use propinstead of attrwhen available. E.g:

根据评论,在可用时使用prop而不是attr。例如:

$("#ans").prop('checked')

回答by SimionBaws

Just use $(selector).is(':checked')

只需使用 $(selector).is(':checked')

It returns a boolean value.

它返回一个布尔值。

回答by Stefan Brinkmann

// use ternary operators
$("#ans").is(':checked') ? 1 : 0;

回答by crashwap

Stefan Brinkmann's answer is excellent, but incomplete for beginners (omits the variable assignment). Just to clarify:

Stefan Brinkmann 的回答很好,但对初学者来说不完整(省略了变量赋值)。只是为了澄清:

// this structure is called a ternary operator
var cbAns = ( $("#ans").is(':checked') ) ? 1 : 0;

It works like this:

它是这样工作的:

 var myVar = ( if test goes here ) ? 'ans if yes' : 'ans if no' ;

Example:

例子:

var myMath = ( 1 > 2 ) ? 'yes' : 'no' ;
alert( myMath );

Alerts 'no'

警报“否”

If this is helpful, please upvote Stefan Brinkmann's answer.

如果这有帮助,请支持 Stefan Brinkmann 的回答。

回答by MAnoj Sarnaik

You can try this:

你可以试试这个:

$('#studentTypeCheck').is(":checked");

回答by alphakevin

I've found the same problem before, hope this solution can help you. first, add a custom attribute to your checkboxes:

我以前也发现过同样的问题,希望这个解决方案可以帮助你。首先,向复选框添加自定义属性:

<input type="checkbox" id="ans" value="1" data-unchecked="0" />

write a jQuery extension to get value:

编写一个 jQuery 扩展来获取值:

$.fn.realVal = function(){
    var $obj = $(this);
    var val = $obj.val();
    var type = $obj.attr('type');
    if (type && type==='checkbox') {
        var un_val = $obj.attr('data-unchecked');
        if (typeof un_val==='undefined') un_val = '';
        return $obj.prop('checked') ? val : un_val;
    } else {
        return val;
    }
};

use code to get check-box value:

使用代码获取复选框值:

$('#ans').realVal();

you can test here

你可以在这里测试

回答by nobody

$('input:checkbox:checked').val();        // get the value from a checked checkbox

回答by Rajesh RK

<input type="checkbox" id="ans" value="1" />

Jquery : var test= $("#ans").is(':checked')and it return true or false.

Jquery : var test= $("#ans").is(':checked')它返回真或假。

In your function:

在您的功能中:

$test =($request->get ( 'test' )== "true")? '1' : '0';

回答by Fareed Alnamrouti

You can also use:

您还可以使用:

$("#ans:checked").length == 1;