简体   繁体   English

当值大于1时,执行此操作

[英]When value is greater than 1, do this

I have a variable named countTicked that is storing an integer of how many relatedBoxes there are on the page. 我有一个名为countTicked的变量,该变量存储页面上有多少个relatedBoxes的整数。

I need an if statement that does something when the value of countTicked is greater than 1. 我需要一个if语句,当countTicked的值大于1时执行某些操作。

if (!$(clickedCheckBox).is(':checked'))
{
     //do something
} 
else
{
    var selectedDots = $(currentMap).find('div.' + checkBoxID);
    var relatedCheckBoxes = $(selectedDots).attr('class');        
    var countTicked = $(relatedCheckBoxes).filter(':checked');

    if (countTicked > 1)
    {
        //do one thing
    } 
    else
    {
        //do something else
    }      
}

I have implemented the changes you guys have provided. 我已经实现了你们提供的更改。 But my second IF statement still doesnt work. 但是我的第二个IF语句仍然不起作用。 What am I still doing wrong? 我仍然在做错什么?

Just use this as if condition: 就像条件一样使用它:

if (countTicked > 1)

countTicked contains an integer, no need to put it in a jquery object. countTicked包含一个整数,无需将其放在jquery对象中。

Why do use use the $ sign? 为什么使用$符号? what is relatedBoxes? 什么是relatedBoxes?

var countTicked = parseInt($(relatedBoxes).length);

if (countTicked > 1)
{
    //do something

} 
else
{
    //do something else
}

Update: you have to use the length to get the number of elements. 更新:您必须使用长度来获取元素数量。

var countTicked = $(relatedCheckBoxes).filter(':checked').length;

it should be : 它应该是 :

  var countTicked = parseInt($(relatedBoxes).length);

            if (countTicked > 1)
            {
                //do something

            } else
            {
                //do something else
            }

as you are using countTicked as variable. 因为您将countTicked用作变量。 No need to use it as a jquery object 无需将其用作jquery对象

$(relatedCheckBoxes).filter(':checked') is a jQuery object, which has length property. $(relatedCheckBoxes).filter(':checked')是一个jQuery对象,具有length属性。 So in order to know amount of ticked checkboxes, use 因此,为了知道已勾选的复选框的数量,请使用

var countTicked = $(relatedCheckBoxes).filter(':checked').length

parseInt isn't obligated. parseInt没有义务。

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

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