views:

172

answers:

3

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?

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

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

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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>

+1  A: 

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.

Ken Browning
A: 

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

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

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.

Tomas Lycken
This would call the function immediately and set its return value as the event handler.
Ken Browning
Sorry - I was a bit too fast. I have edited to correct the mistake.
Tomas Lycken
Although that is actually the mistake that the OP has made as well =)
Tomas Lycken
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())`
Ken Browning