2

I used jquery to set the checked/unchecked the checkboxes with same class name. I searched web at jquery set checkbox checked. I tried to using prop and attr too. However it doesn't work for me, but I can disable the checkbox. Would someone tell me what I missing.

There is the jquery I used:

 <script language="javascript" src="jquery-2.1.4.js" type="text/javascript"></script>
<script language="javascript" src="jquery-ui-1.11.4/jquery-ui.js" type="text/javascript"></script>

There are the checkbox in page:

<asp:CheckBox ID="chkOne" runat="server" Text="One"  CssClass="optionsBoxes"/>
<asp:CheckBox ID="chkTwo" runat="server" Text="Two"  CssClass="optionsBoxes"/>
<asp:CheckBox ID="chkThree" runat="server" Text="Three"  CssClass="optionsBoxes"/>
<asp:CheckBox ID="chkFour" runat="server" Text="Four"  CssClass="optionsBoxes"/>

My jquery:

$('.optionsBoxes').attr('checked', false);    
$('.optionsBoxes').prop('checked', true);                  
$('.optionsBoxes').attr('disabled', true);  //worked
Y Chan
  • 317
  • 2
  • 18
  • maybe it is not possible, look at this question: https://stackoverflow.com/questions/1523606/asp-net-checkbox-value-at-postback-is-wrong – Sysix Oct 05 '18 at 18:38

1 Answers1

1

Does

$('.optionsBoxes')

result in an array of input[type="checkbox"] elements?

You can try this approach:

$('.optionsBoxes').each(function() { $(this).prop('checked', false) });
  • I tried your code but it still doesn't work. I added the jquery version in my question. It the version I used cause the problem? Thanks. – Y Chan Oct 05 '18 at 18:13
  • 1
    @eewardbrosens, I used the developer tool to check the checkbox element. It shows that . The span cause the problem. – user819774 Oct 09 '18 at 13:28
  • I used the nextUntil and closest for looking the input tag, but it still doesn't work. var chk = $(this).closest("input"); chk.prop('checked', false); – user819774 Oct 09 '18 at 13:48
  • Can you show the exact html output in your question? – edwardbrosens Oct 10 '18 at 14:10