我的Javascript知识非常有限,所以需要一些帮助:
我有以下功能:
$('#addDialog').dialog({
autoOpen: false,
width: 500,
buttons: {
"Add": function() {
//alert("sent:" + addStartDate.format("dd-MM-yyyy hh:mm:ss tt") + "==" + addStartDate.toLocaleString());
var eventToAdd = {
//title: $("#addEventName").val(),
title: $("#addEventSalesPerson option:selected").text(),
description: $("#addEventDesc").val(),
start: addStartDate.format("dd-MM-yyyy hh:mm:ss tt"),
end: addEndDate.format("dd-MM-yyyy hh:mm:ss tt"),
salesperson: $("#addEventSalesPerson option:selected").text(),
eventPostCode: $("input[id$=addEventPostCode]").val(),
eventname: $("#addEventEventName option:selected").text()
};
if ($("input[id$=addEventPostCode]").val().length < 5) {
alert("Post code cannot be blank and must contain a minimum of 5 characters");
}
else {
//alert("sending " + eventToAdd.title);
PageMethods.addEvent(eventToAdd, addSuccess);
$(this).dialog("close");
}
}
}
});
#addEventEventName
是一个从SQL填充的DDL,有几个选项。目前,如果"input[id$=addEventPostCode]"
少于5个字符,则会发出提醒。
我需要的是,如果选择的选项是假日或疾病,那么它不会显示警报。感谢
更新 我按照@David的建议尝试添加以下行,但仍然没有喜悦 - 任何接受者?
if ($("input[id$=addEventPostCode]").val().length < 5 && !($("#addEventSalesPerson option:selected").text() == "Holiday" || $("#addEventSalesPerson option:selected").text() == "Sickness")) {
答案 0 :(得分:1)
更改以下行
if ($("input[id$=addEventPostCode]").val().length < 5) {
到
if ($("input[id$=addEventPostCode]").val().length < 5 && !($("#addEventSalesPerson option:selected").text() == "Holiday" || $("#addEventSalesPerson option:selected").text() == "Sickness")) {
如果不是假日或疾病,它应该只发出警告
! //This is Not
( //Parenthesis group expressions together so the not works on the result of the expressions inside
$("#addEventSalesPerson option:selected").text() == "Holiday" //This checks if selected text matches string
|| //This means Or
$("#addEventSalesPerson option:selected").text() == "Sickness" //This checks if selected text matches string
)
如果任何字符串匹配,结果将为false,if将不会触发警报。