我是新來的jQuery,但這是我想要做的。jQuery比較提交表單前選擇字段值與數組
我有一個多選擇字段命名城市和Zip形式,
我試圖讓這個選擇字段的值,並將其與城市的名單上有,如果發現任何匹配的jQuery將使用這些值更新名爲city的隱藏文本字段的值,然後提交表單。如果找不到匹配,他們將被重定向到不同的網站。
與郵政編碼相同的東西,它會從同一字段獲取郵政編碼,並查看它是否與列表中的任何郵政編碼匹配。如果是,則更新郵政編碼隱藏字段,如果沒有,則將其重定向到另一個網站。
這是我現在的代碼,它不工作。
$("#front-form").submit(function(event) {
// Getting the values from the HTML select field
var CityandZip = $("#citynzip").val();
//Comparing the values with the cities list
if (CityandZip === "City 1" || "City 2" || "City 3") {
//Updaing the hidden field and submitting
$("#HiddenCityField").val(CityandZip);
//Comparing the values with the ZIP code list
} else if (CityandZip === "11111" || "11112" || "11113") {
//Updating the hidden ZIP code field
$("#HiddenZipField").val(CityandZip);
} else {
//Redircting to a new site
window.location.href = "http://www. redirecttowebsite.com";
}
});
任何幫助將不勝感激,並感謝先進。
你的||是錯的(CityandZip ===「11111」|| CityandZip ===「11112」|| CityandZip ===「11113」)使用調試器,你會很容易地發現這種錯誤。 – 2013-02-19 20:56:10