Not able to get selected value for Multiple asp:Ra

2019-08-06 09:05发布

I wonder if someone has stumbled upon this problem.

I have two asp:RadioButtonList controls on the same page. I gave them both a click event via jQuery (see below).

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

   });
}

My problem is that when I click on the first RadioButtonList the selected value is correct, but as soon as I click on the second RadioButtonList the selected value stays the same as the first selected value.

How can I fix this problem?

1条回答
爷的心禁止访问
2楼-- · 2019-08-06 09:46

I found the solution.

I just need to change the way i search for the jQuery selected value instead of:

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();

I used:

var rad_link_type = $('#<%= rad_link_type.ClientID%>').find(":checked").val();
var rad_banner_type = $('#<%= rad_banner_type.ClientID%>').find(":checked").val();
查看更多
登录 后发表回答