views:

22

answers:

1

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?

+2  A: 

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
});
Nick Craver
Thanks Nick. While you were answering, I came up with a different approach which worked as well but yours is more correct.This was my approach:$("input[id|=Record],[id$=Action]")
Hallaghan
@Hallaghan - Ah yes, that's a different starts with, either way works :) Your .DropDownList is rendering as an `<input>` though? It should be a `<select>`, or I may be going crazy...
Nick Craver
It renders as a <select> but input worked fine :P
Hallaghan
@Hallaghan - Are you sure you're not using `":input"`? :)
Nick Craver
Absolutely sure :)
Hallaghan