简体   繁体   English

请尝试检查是否已选中复选框,我在做什么错?

[英]Trying to check if a checkbox is checked, please, what am i doing wrong?

$(document).ready(
function()
{   
    $('input[id^="btnRemoveCategory"]').bind(
        'click',
        function(){
            if($('input[id^="chkIsBottom_":checked]'))
                alert('YES YES YES!!!!');
            else
                alert('No NO no!!!!!');

I've tried with the: 我已经尝试过:

if($('input[id^="chkIsBottom_"]').attr(checked))

also.... 也....

where is my mistake??? 我的错误在哪里???

10x 10倍

$('input[id^="chkIsBottom_":checked]')

… is always going to return a true value. ……总是会返回一个真实值。 If none are checked, then it will be a jQuery object with no element inside it. 如果未选中任何对象,则它将是一个内部没有元素的jQuery对象。 You want: 你要:

$('input[id^="chkIsBottom_":checked]').length

(which will be false if the length is 0, and true if it isn't) (如果长度为0,则为false,否则为true)

Meanwhile: 与此同时:

if($('input[id^="chkIsBottom_"]').attr(checked))

… will get the value of the X attribute of the first input element with an id that starts with chkIsBottom_, where X is whatever the variable checked contains (which is probably undefined, since you probably intended to pass a string). …将获取第一个输入元素的X属性的值,其ID以chkIsBottom_开头,其中X是所检查的变量包含的任何内容(由于可能要传递字符串,因此未定义)。

You were very close with your first implementation. 您与第一个实现非常接近。

Try this adjustment: 尝试此调整:

$(document).ready(
function()
{   
    $('input[id^="btnRemoveCategory"]').bind(
        'click',
        function(){
            if($('input[id^="chkIsBottom_"]:checked').length > 0)
                alert('YES YES YES!!!!');
            else
                alert('No NO no!!!!!');

Another option: 另外一个选项:

if($('input[id^="chkIsBottom_"]').is(':checked'))

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

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