2011-03-02 103 views
0

我有非常有限的JavaScript知識,需要一些幫助,請:JavaScript的alert(下拉列表)

我有以下功能:

$('#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是填充的DDL SQL並有幾個選項。目前,如果"input[id$=addEventPostCode]"少於5個字符,則會發出警報。

我需要的是,如果選擇的選項假日疾病那麼它不會顯示一個警告。謝謝

更新 我試着按@戴維的建議添加下面的行,但仍然沒有喜悅 - 任何接受者?

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

回答

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 
) 

如果任何字符串匹配結果將是錯誤的,我f不會觸發警報。

+0

如果我選擇假日或疾病,它仍然會要求郵政編碼。這裏是一行代碼:'if($(「input [id $ = addEventPostCode]」).val().length <5 &&!($(「#addEventSalesPerson option:selected」).text()==「假日「|| $(」#addEventSalesPerson option:selected「)。text()==」Sickness「)){alert(」發佈代碼不能爲空並且必須包含至少5個字符「); }' – James 2011-03-02 19:18:34