如何在此声明中添加另一个不相等(!=)?

时间:2012-11-23 18:47:09

标签: javascript if-statement equals key-value

如何在此语句中添加另一个不等于(!=)的值?

if ($(this).data("increase_priority1") && $(this).val() != 1) 

我尝试在函数开头反转语法,确定它是否相等,但是它阻止它完全删除项目(包括那些不等于1的项目)

if ($(this).data("increase_priority1") && $(this).val() != 1 && $(".complaint select").val() != "Too_small")

当用户选择投诉并对问题的重要性级别进行排名时,此功能会添加和/或删除“increase_priority1”中的值,我需要它来更改值(在这种情况下投诉是什么)和如果这两个字段中的任何一个发生变化,则重要性级别(即increase_priority1)。目前它仅在重要性级别发生变化时才会发生变化。

完整的功能是:

var $increase_priority1 = $(".increase_priority1");
$('.ranking, .complaint select').dropkick({
change: function () {
    var name = $(this)
        .data("name"); //get priority name
    if ($(".complaint select")
        .val() === "Too_small" && $(this)
        .val() == 1 && !$(this)
        .data("increase_priority1")) {
        //rank is 1, and not yet added to priority list
        $("<option>", {
            text: name,
            val: name
        })
            .appendTo($increase_priority1);
        $(this)
            .data("increase_priority1", true); //flag as a priority item
    }
    if ($(this)
        .data("increase_priority1") && $(this)
        .val() != 1) {
        //is in priority list, but now demoted
        $("option[value=" + name + "]", $increase_priority1)
            .remove();
        $(this)
            .removeData("increase_priority1"); //no longer a priority item
    }
}
});

小提琴在上下文中显示:http://jsfiddle.net/chayacooper/vWLEn/132/

1 个答案:

答案 0 :(得分:2)

当至少有一个操作数为真时(可能两者都是!),OR运算成立。你的陈述应该是:

if ($(this).data("increase_priority1") && 
    ($(this).val() != 1 || $(".complaint select").val() != "Too_small")). 

||是OR的Javascript语法。

如果if为真 .data("increase_priority1") $(this).val() != 1为真,则会运行$(".complaint select").val() != "Too_small")

请注意,如果&&的第一部分为假,解释器将停止,也就是说:它甚至不会查看第二部分。 ||也是如此,但反过来说,如果||的第一部分为真,则不会查看第二部分。

相关问题