javascript - Set the selected index of a dynamic dropdown(DOM) using jQuery -


1)model

my index values stored in int array;

public int[] mobileid { get; set;}  

2)view

@for(int i=0;i<3;i++)//now create 3 dropdown same name { <div> @html.dropdownlistfor(model=>model.mobileid ,new selectlist((viewbag.mobileinfo),"value","text"),new {@id="ddl"}) </div> } 

3)script

mobileid have 3 integer value. values 2, 3 4 how set selected value above 3 dropdown

$("#ddl").val('2'); $("#ddl").val('3'); $("#ddl").val('4'); 

when dropdownload ,this values show 1st position

you can't have 3 select boxes same id. it's invalid. id values must unique.

instead, either give them different ids, e.g.:

@for(int i=0;i<3;i++)//now create 3 dropdown same name { <div> @html.dropdownlistfor(model=>model.mobileid ,new selectlist((viewbag.mobileinfo),"value","text"),new {@id="ddl" + i}) // ------------------------------------------------------------------------------------------------------------^^^^ </div> } 

...and use $("#ddl0"), $("#ddl1"), etc.

or give them common name or class, , use jquery's .eq(n) choose each of them. names: $("[name=ddl]").eq(0), $("[name=ddl]").eq(1) etc.; class: $(".ddl").eq(0), $(".ddl").eq(1)


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -