开发者

how to get next relative dropdown using jquery

i have this code

<div id="aggregate" style="display:inline">
                    <%=Html.RadioButton("a", "1", true, new {id = "meRadio"})%><label>Me</label>
                    <%=Html.RadioButton("a", "2", false, new { id = "teamRadio" })%><label>Team: </label><% = Html.DropDownList("Teams", Model.Teams,  new {@scope="Team",  @id = "teamDropdown", @class = "filterDropdown" })%>
                    <%=Html.RadioButton("a", "4", false, new { id = "managerRadio" })%><label>Manager: </label><% = Html.DropDownList("Managers", Model.People, new { @scope = "Manager", @id = "managerDropdown", @class = "filterDropdown" })%>
                    <%=Html.RadioButton("a", "5", false, new { id = "locationRadio" })%><label>Location: </label><% = Html.DropDownList("Locations", Model.Locations, new { @scope = "Location", @id = "locationDropdown", @class = "filterDropdown" })%>
  </div>

i currently have code like this:

 $('#workstreamRadio').click(function () {
     Enable("#workstreamDropdo开发者_运维知识库wn");
 });

 $('#teamRadio').click(function () {
     Enable("#teamDropdown");
 });

I am sure there is a way using jquery to dynamically get the respective dropdown selector given a specific radio button clicked so i dont have to hard code each mapping.

what is the syntax for saying "get me the selector of the dropdown to the right of the radio button i clicked"


You can do it using .nextAll() and :first like this:

$("#aggregate :radio").change(function() {
  var dropdown = $(this).nextAll("select:first");
  //enable or disable dropdown
});

since they're siblings, use .nextAll() which goes through all siblings, and you want the first <select> element, so select that from the siblings after the radio clicked. Also look at using .change() here instead of .click(), it'll fire only when the selection changes, which is usually what you want here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜