jQuery compare select field values with an array before submitting form -



jQuery compare select field values with an array before submitting form -

i'm new jquery here i'm trying do.

i have form multi select field named city , zip,

i'm trying values of select field , compare against list of cities have, if found match jquery update value of hidden text field named city these values , submit form. if no match found redirected different website.

same thing zip codes, zip codes same field , see if matchs zip code list. if yes update zip code hidden field, if no redirect them website.

here code right , it's not working.

$("#front-form").submit(function(event) { // getting values html select field var cityandzip = $("#citynzip").val(); //comparing values cities list if (cityandzip === "city 1" || "city 2" || "city 3") { //updaing hidden field , submitting $("#hiddencityfield").val(cityandzip); //comparing values zip code list } else if (cityandzip === "11111" || "11112" || "11113") { //updating hidden zip code field $("#hiddenzipfield").val(cityandzip); } else { //redircting new site window.location.href = "http://www. redirecttowebsite.com"; } });

any help appreciated , in advanced.

i checked out. did cities check, not zip code check, building off existing implementation.

$(function () { var arrcities = [ "philadelphia", "pittsburgh", "nyc" ]; $("#front-form").submit(function (event) { $("#citynzip :selected").each(function (i, selected) { if ($.inarray($(selected).text().trim(), arrcities) > -1) { $("#hiddencityfield").val($(selected).text()); } else { window.location.href = "http://www. redirecttowebsite.com"; } }); }); });

going on did. first off, have base of operations array of cities. threw in here. attach form submit event had. utilize jquery selectors select elements of citynzip element selected , perform .each() on each of selected elements. status checks if selected item in array using jquery's .inarray() function check if selected item in base of operations array compare to, arrcities. if is, add together hidden field.

i don't know how want handle if selections not in array. right looks if of selections not in array, if are, want page redirected , go on if of selections made user exist in array. should provide starting point.

jquery arrays if-statement validation multi-select

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -