无法使用jQuery为多个asp:RadioButtonList获取选定值

时间:2014-01-22 08:57:48

标签: jquery asp.net

我想知道是否有人偶然发现了这个问题。

我在同一页面上有两个asp:RadioButtonList控件。我通过jQuery给了他们两个点击事件(见下文)。

Default.aspx的:

<asp:RadioButtonList ID = "rad_banner_type" runat = "server" >
   <asp:ListItem Value ="Picture" Text ="Image(.jpg .png .bpm)" />
   <asp:ListItem Value ="Code" Text ="Code" />
   <asp:ListItem Value ="Flash" Text ="Flash(.swf)" />
</asp:RadioButtonList>

jQuery的:

function pageLoad() {
   //LINK TYPE
   $("#<%= rad_link_type.ClientID%>").change(function () {
     var rad_link_type = $("input[@name=<%=rad_link_type.ClientID%>]:radio:checked").val();

        switch (rad_link_type) {
           case "Email":
              //Do Something
              break;
           case "PDF":
              //Do Something
              break;
           case "Website":
              //Do Something
              break;
           default:
              //Do Something
              break;
        }
   });

   //PICTURE TYPE
   $("#<%= rad_banner_type.ClientID%>").click(function () {
      var rad_banner_type = $("input[@name=<%=rad_banner_type.ClientID%>]:radio:checked").val();


         switch (rad_banner_type) {
            case "Picture":
               //Do Something
               break;
            case "Code":
               //Do Something
               break;
            case "Flash":
                //Do Something
                break;
            default:
                //Do Something
                break;
         }

   });
}

我的问题是,当我点击第一个RadioButtonList时,所选值是正确的,但是当我点击第二个RadioButtonList时,所选值与第一个选定值保持一致。

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。

我只需要改变搜索jQuery选择值的方式 而不是:

var rad_link_type = $("input[@name=<%=rad_link_type.ClientID%>]:radio:checked").val();
var rad_banner_type = $("input[@name=<%=rad_banner_type.ClientID%>]:radio:checked").val();

我用过:

var rad_link_type = $('#<%= rad_link_type.ClientID%>').find(":checked").val();
var rad_banner_type = $('#<%= rad_banner_type.ClientID%>').find(":checked").val();