简体   繁体   English

jQuery if(x == y)不起作用

[英]jQuery if (x == y) not working

So, I have some faux checkboxes (so I could style them) that work with jQuery to act as checked or not checked. 因此,我有一些伪造的复选框(以便为它们设置样式),这些复选框可与jQuery一起使用,以充当已选中或未选中状态。 There are a number of faux checkboxes in my document, and for each one I have a click function: 我的文档中有许多人造复选框,每一个都有一个单击功能:

var productInterest = [];
productInterest[0] = false;
productInterest[1] = false;
productInterest[2] = false;

// here is one function of the three:

$('#productOne').click(function() {
    if (productInterest[0] == false) {
        $(this).addClass("checkboxChecked");
        productInterest[0] = true;
    } else {
        $(this).removeClass("checkboxChecked");
        productInterest[0] = false;
    }
});

The problem seems to be that there is an error in the if statement, because it will check, but not uncheck. 问题似乎是if语句中存在错误,因为它将检查而不是取消检查。 In other words it will add the class, but the variable won't change so it still thinks its checked. 换句话说,它将添加类,但变量不会更改,因此它仍然认为已选中。 Anybody have any ideas? 有人有什么想法吗? Thanks for your help. 谢谢你的帮助。

UPDATE: So, I need to show you all my code because it works in the way I supplied it (thanks commenters for helping me realize that)... just not in the way its actually being used on my site. 更新:因此,我需要向您展示我的所有代码,因为它可以按照我提供的方式工作(感谢评论员帮助我实现了这一点)……只是,并不是真正在我的网站上使用它的方式。 so below please find the code in its entirety. 因此,请在下面找到完整的代码。

Everything needs to happen in one function, because the UI and data for each checkbox need to be updated at once. 一切都需要在一个函数中进行,因为每个复选框的UI和数据都需要立即更新。 So here is the complete function: 所以这是完整的功能:

$('input[name=silkInterest]').click(function() { // they all have the same name

    var silkInterest = [];
    silkInterest[0] = false;
    silkInterest[1] = false;
    silkInterest[2] = false;

    if ($(this).is('#silkSilk')) { // function stops working because the .is()
        if (silkInterest[0] == false) {
            $(this).addClass("checkboxChecked");
            silkInterest[0] = true;
        } else {
            $(this).removeClass("checkboxChecked");
            silkInterest[0] = false;
        }
        alert(silkInterest[0]);
    }
    if ($(this).is('#silkAlmond')) {
        if (silkInterest[1] == false) {
            $(this).addClass("checkboxChecked");
            silkInterest[1] = true;
        } else {
            $(this).removeClass("checkboxChecked");
            silkInterest[1] = false;
        }
    }
    if ($(this).is('#silkCoconut')) {
        if (silkInterest[2] == false) {
            $(this).addClass("checkboxChecked");
            silkInterest[2] = true;
        } else {
            $(this).removeClass("checkboxChecked");
            silkInterest[2] = false;
        }
    }

    var silkInterestString = silkInterest.toString();
    $('input[name=silkInterestAnswer]').val(silkInterestString);
    // This last bit puts the code into a hidden field so I can capture it with php.

});

I can't spot the problem in your code, but you can simply use the class you're adding in place of the productInterest array. 我无法在您的代码中发现问题,但是您可以简单地使用要添加的类代替productInterest数组。 This lets you condense the code down to a single: 这使您可以将代码压缩为单个代码:

// Condense productOne, productTwo, etc...
$('[id^="product"]').click(function() {
    // Condense addClass, removeClass
    $(this).toggleClass('checkboxChecked');
});

And to check if one of them is checked: 并检查是否其中之一被选中:

if ($('#productOne').hasClass('checkboxChecked')) {...}

This'll make sure the UI is always synced to the "data", so if there's other code that's interfering you'll be able to spot it. 这样可以确保UI始终与“数据”同步,因此,如果有其他代码在干扰您,您将可以发现它。

Okay, just had a palm to forehead moment. 好吧,只是手掌到额头的瞬间。 In regards to my revised code- the variables get reset everytime I click. 关于我修改后的代码-每次单击都会重置变量。 That was the problem. 那就是问题所在。 Duh. 咄。

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

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