javascript - if/switch statement based on a select value and array? -


i have simple select element multiple values groups in 4 categories. try , more efficient thought best group them in arrays , check if "selected" value in 1 of them, printing different text based on arrays select value in. wouldn’t more efficient switch?

maybe it's bit vague. user starts dropdown multiple values(for example: x1,y2,x3,y4,z1) wright statement if x1,x3 something, if y2,y4 something. thought cleaner put values in arrays (for example arrayx = x1,x3 , arrayy=y2,y4.) script check array contains selected value, , print custom message each array.

after tinkering,this working example:

  $( "#search2" )         .change(function() {             var group1 = ['pl'];             var group2 = ['ad','rs','ch'];             var group3 = ['at','be','bg','hr','cy','cz','dk','ee','fi','fr','gr','es','nl','ir','is','lt','lu','lv','mt','de','no','pt','ro','sk','si','se','hu','gb','it'];             var group4 = ['sm','mc','li'];             var group5 = ['qq'];              $("#search2 option:selected").each(function () {                 str2 += $(this).val();                 if ($.inarray(str2, group1) !== -1) {                     groupnr ="1";                 }                 else if ($.inarray(str2, group2) !== -1) {                     groupnr = "2";                 }                 else if ($.inarray(str2, group3) !== -1){                     groupnr = "3";                 }                 else if ($.inarray(str2, group4) !== -1){                     groupnr = "4";                 }                 else if ($.inarray(str2, group5) !== -1){                     groupnr = "5";                 }             });             if($('#search2').val()){                 $('#submit').removeclass('hidden');             }         })         .trigger( "change" ); 

their easier way. cna have array , assign each value group. like

mygrouparray['ia'] = 1,     mygrouparray['ks'] = 1 mygrouparray['mn'] = 2 , on 

then can this

groupnumber = mygrouparray[optionvalue] === undefined ? 4: mygrouparray[optionvalue]

this quicker , easy way. since 1 comparison. bit more on initialization.

please let me know if understood.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -