如何使用jquery获取下一个相对下拉列表

时间:2010-08-29 11:47:16

标签: jquery jquery-selectors css-selectors

我有这个代码

<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>

我目前有这样的代码:

 $('#workstreamRadio').click(function () {
     Enable("#workstreamDropdown");
 });

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

我确信有一种方法可以使用jquery动态获取相应的下拉选择器,只要点击一个特定的单选按钮,所以我不必硬编码每个映射。

说“让我点击单击的单选按钮右侧下拉列表的选择器”是什么语法

1 个答案:

答案 0 :(得分:2)

你可以使用.nextAll():first这样做:

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

因为他们是兄弟姐妹,所以使用遍历所有兄弟姐妹的.nextAll(),并且你想要第一个<select>元素,所以在点击无线电后从兄弟姐妹中选择它。另外,请在此处使用.change()而不是.click(),只有在选择更改时才会触发,这通常是 您想要的内容。