Tutorial :Selecting all dropdowns



Question:

Inside my form, I've declared dropdows the following way:

Html.DropDownList(String.Format("Record[{0}].Action", i), new[]  {      new SelectListItem { Text="Ajustar Quantidade", Value= ((int)InventoryGoodsActionEnum.AdjustQuantity).ToString()},      new SelectListItem { Text="Relocalizar", Value= ((int)InventoryGoodsActionEnum.AdjustLocation).ToString(), Selected=true},      new SelectListItem { Text="Ajustar Quantidade e Relocalizar", Value= ((int)InventoryGoodsActionEnum.AdjustQuantityLocation).ToString()},      new SelectListItem { Text="Ignorar", Value= ((int)InventoryGoodsActionEnum.Ignore).ToString()},  })  

Now I'd like to be able to get all of them (they will be multiple because the id increases) with jquery so I can iterate through them. How can I do this?


Solution:1

Use a tag/element selector and an attribute starts-with selector, like this:

$("select[name^='Record[']")each(function() {    //do something  });  

This will select all dropdowns with a name="Record[...." to loop through. If necessary you can add a ends-with selector as well, like this:

$("select[name^='Record['][name$='].Action']")each(function() {    //do something  });  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »