浙江省高等学校教师教育理论培训

微信搜索“教师资格证岗前培训”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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') {	       
              }
              posted on 2010-11-15 21:30  lexus  阅读(428)  评论(0编辑  收藏  举报