0

I was given the code below to disable a button on an ASP.Net page if none of the checkboxes are checked, and to enable it if any checkboxes get checked. It disables the button fine when the page loads and no boxes are checked, but never hits the $('.cb').change(setButton) code to enable the button when I check one of the checkboxes. Anyone know what might be wrong?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:CheckBox ID="CheckBox1" runat="server" CssClass="cb" />
        <asp:CheckBox ID="CheckBox2" runat="server" CssClass="cb" />
        <asp:Button ID="Button1" runat="server"  CssClass="button"
        Text="Button" />

</div>
</form>
<script language="jquery" src="js/jquery-1.3.2.js" type="text/javascript"> 
</script>
<script type="text/javascript" >

    $(document).ready(function() {

        function setButton() {
            $('#Button1').attr('disabled', $('.cb:checked').length === 0);
        }

        // run check after changing any of the checkboxes
        $('.cb').change(setButton);

        // initial check
        setButton();

    });

</script>

3 Answers 3

1

Your problem is that you're attaching a handler to the change event instead of the click event.

There's another problem wherein asp.net doesn't put the CssClass of your <asp:CheckBox> on the rendered <input> element, but instead on a containing <span> element. That behavior might be specific to the .net framework version which I use but you might inspect your rendered HTML to verify that your jQuery selectors are accurate.

Sign up to request clarification or add additional context in comments.

Comments

0

Do the buttons have class="cb"

have you tried putting in an alert/console.log statement in the

$('.cb').click(function(){ alert 'got here'; setButton });

2 Comments

just noticed i had click and you had change. Ken pointed it out explicitly.
Added the alert and that works, but the setButton function isn't getting called, or isn't working correctly.
0

Try adding a pair of parenthesis on the call to setButton:

$('.cb').click(function() {setButton()});

Also, you might want to try debugging your code using Firebug.

4 Comments

This would call the function immediately and set its return value as the event handler.
Sorry - I was a bit too fast. I have edited to correct the mistake.
Although that is actually the mistake that the OP has made as well =)
He's passing in the function, not the results of a call to that function. Your current edit's code and his are doing the same thing. you're just wrapping a call to setButton in an anonymous function. $('.cb').change(setButton) is not the same as $('.cb').change(setButton())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.