zoukankan      html  css  js  c++  java
  • check radio button is checked use jquery

    Checking if certain Radiobutton is checked

    作者: 
     在 03-Feb-2010 03:46 PM.
      在  Using jQuery 
    I've got the following code:
     
    <input type="radio" runat="server" name="testGroup" id="test1" /><label for="<%=test1.ClientID %>" style="cursor:hand" runat="server">Test1</label>
    <input type="radio" runat="server" name="testGroup" id="test2" /><label for="<%=test2.ClientID %>" style="cursor:hand" runat="server">Test2</label>
    <input type="radio" runat="server" name="testGroup" id="test3" /> <label for="<%=test3.ClientID %>" style="cursor:hand">Test3</label> 
     
    And then this to check if the 2nd radio button is checked:
     

    return

    $('#test2').attr('checked');It's returning 'undefined' and I'm not sure why.
    状态

    已回答

      2个用户有相同的咨询 
     

    Re: Checking if certain Radiobutton is checked

    作者: 
     在 03-Feb-2010 03:51 PM
    well when the checkbox isn't checked there isn't a attribute called checked.

    when it is checked there will be a checked="checked" attribute.

    Your test should be:

    Copy code
    1. var _test2 = $("#test2");
    2. return (_test2.attr("checked") != "undefined" && _test2.attr("checked") == "checked");
       
      Alternatively, to get the currently selected radio button's id:

      Copy code
      1. var id = $("input[@name=testGroup]:checked").attr('id');

      Or if each input has a unique 'value' attribute:

      Copy code
      1. var value = $("input[@name=testGroup]:checked").val();
         

        Re: Checking if certain Radiobutton is checked

        作者: 
         在 03-Feb-2010 04:08 PM
        Thanks.  I'll try this out again.  But won't the following give me the value attribute's value? <input value="valueOfInput ...>:

        $("input[@name=testGroup]:checked").val();

        if that's the case then I'd have to do something like this?

        return $("input[@name=testGroup]:checked").val() == "valueOfInput";
           

          Re: Checking if certain Radiobutton is checked

          作者: 
           在 03-Feb-2010 04:10 PM

          example:
           
          <input type="radio" runat="server" name="radioGroup"  id="payPalRadioButton" value="paypalSelected" />
           
          this returns 'undefined' still:
           
          $("input[@name=radioGroup]:checked").attr('payPalRadioButton');
           
          and this returns false no matter if I HAVE selected that certain radio button above:


          alert($('input:radio[name=payTypeGroup]:checked').val() == 'paypalSelected')

             

            Re: Checking if certain Radiobutton is checked

            作者: 
             在 03-Feb-2010 08:58 PM
            they way i do it is

            Copy code
            1. bFlag = $('#test2').is(':checked');
               
              Your example looks like .NET code. Isn't the id value in the control only valid for the server side of the control? That's why you're using test2.ClientID in the label, so the client-side HTML will use the right id. You'd need to do the same for your jQuery selectors as well.
                 

                Re: Checking if certain Radiobutton is checked

                作者: 
                 在 02-Nov-2010 08:58 PM
                if ($('#test2:checked').val() == 'true') {	       
                }
              • 相关阅读:
                函数探幽--引用变量
                函数探幽—内联函数
                我遇到的头文件
                继承的特点
                汇编语言中压栈与入栈
                cin.good(),cin.fail(),cin.clear()
                结构体的处理(以c++primer plus 第六章习题4为例)
                uva508
                uva253 CubePainting
                uva1590
              • 原文地址:https://www.cnblogs.com/lexus/p/1877971.html
              Copyright © 2011-2022 走看看