开发者

Javascript alert based on specific selected inputs (drop down lists)

I have very limited Javascript knowledge so require some help please:

I have the following function:

$('#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 is a DDL populated from SQL and has several options. Currently, if the "input[id$=addEventPostCode]" has less than 5 characters then it gives an Alert.

What I need is, If the Selected Option is Holiday or Sickness then it does not display an alert. Thanks

Update I tried adding the following line as per @David's suggestion but still no joy - any takers?

if ($("input[id$=addEventPostCode]").val().length < 5 && !($("#addEventSalesPerson option:selected").text() == "Holiday" || $("#addEventSalesPerson option:selected").text() == "Sickness")) {


Change the following line

if ($("input[id$=addEventPostCode]").val().length < 5) {

to

if ($("input[id$=addEventPostCode]").val().length < 5 && !($("#addEventSalesPerson option:selected").text() == "Holiday" || $("#addEventSalesPerson option:selected").text() == "Sickness")) {

And it should only alert if not Holiday or 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
)

If any of the strings match the result will be false and the if will not trigger the alert.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜