【发布时间】:2012-11-21 02:49:50
【问题描述】:
如何将另一个要按值评估的元素添加到此语句中?
if ($(this).val() == 1 && !$(this).data("priority1"))?
我想使用 class 作为这个新元素的选择器(在这种特殊情况下为 class="complaint",其中一个值为 "Too_small")
我试过了(都带&不带括号)
if ($('.complaint').val() === "Too_small" && $(this).val() == 1 && !$(this).data("priority1"))
我已经包含了下面的函数,以及一个工作示例http://jsfiddle.net/chayanyc/Dhaat/225/的小提琴
var $priority1 = $(".priority1");
$(".features_rank1").change(function() {
var name = $(this).data("name"); //get priority name
if ($(this).val() == 1 && !$(this).data("priority1")) {
//rank is 1, and not yet added to priority list
$("<option>", {text:name, val:name}).appendTo($priority1);
$(this).data("priority1", true); //flag as a priority item
}
if ($(this).data("priority1") && $(this).val() != 1) {
//is in priority list, but now demoted
$("option[value=" + name + "]", $priority1).remove();
$(this).removeData("priority1"); //no longer a priority item
}
});
【问题讨论】:
-
我不知道我是否在这里犯了错误,但是,为什么要使用“===”而不是只使用“==”?
-
@GerardDiaz
==确实类型强制,通常你总是想在 Javascript 中使用===
标签: jquery if-statement key-value