最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Allow only one (approvereject) checkbox to be checked - Stack Overflow

programmeradmin2浏览0评论

updated:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="chkbox_checked_uncheked.aspx.cs"
    Inherits="Ediable_Repeater.chkbox_checked_uncheked" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ".dtd">
<html xmlns="">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>

    <script type="text/javascript">

        $(document).ready(function () {
            $('#C1All').click(function () {
                debugger
                $('.col1').attr("checked", $('#C1All').attr("checked"));
                $('.col2').removeAttr("checked");
                $('#C2All').removeAttr("checked");
            });

            $('#C2All').click(function () {
                debugger
                $('.col2').attr("checked", $('#C2All').attr("checked"));
                $('.col1').removeAttr("checked");
                $('#C1All').removeAttr("checked");
            });

            $('.col1').each(function () {
                $(this).click(function () {
                    var id = $(this).attr('id');
                    debugger
                    var coresId = id.replace('C1', 'C2');
                    $('#' + coresId).removeAttr("checked");
                    $('#C1All').removeAttr("checked");
                    $('#C2All').removeAttr("checked");
                });
            });

            $('.col2').each(function () {
                $(this).click(function () {
                    var id = $(this).attr('id');
                    var coresId = id.replace('C2', 'C1');
                    debugger
                    $('#' + coresId).removeAttr("checked");
                    $('#C1All').removeAttr("checked");
                    $('#C2All').removeAttr("checked");
                });
            });
        });
  </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            <table border="1">
                <tr>
                    <td>
                        <asp:CheckBox ID="C1All" runat="server" class="col1" Text="approve all" />
                    </td>
                    <td>
                        <asp:CheckBox ID="C2All" runat="server"  class="col2" Text="Reject all" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:CheckBox ID="C101" runat="server" class="col1" Text="john 0" />
                    </td>
                    <td>
                        <asp:CheckBox ID="C201" runat="server"  class="col2" Text="john 0" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:CheckBox ID="C102" runat="server" class="col1" Text="john 1" />
                    </td>
                    <td>
                        <asp:CheckBox ID="C202" runat="server"  class="col2" Text="john all" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:CheckBox ID="C103" runat="server" class="col1" Text="john 2" />
                    </td>
                    <td>
                        <asp:CheckBox ID="C203" runat="server"  class="col2" Text="John 2" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    </form>
</body>
</html>

i have a two columns (approve all/reject all) how can i restrict the user to allow only one checkbox of each?

here is the output of the screen:

i have tried something like this but did not work:

 function SelectApproveAllCheckboxes(chk, selector) 
    {     
         $('#<%=gv.ClientID%>').find(selector + " input:checkbox").each(function () 
        {                    
              $(this).prop("checked", $(chk).prop("checked"));     
         }); 
    } 
function SelectRejectAllCheckboxes(chk, selector) 
{     
     $('#<%=gv.ClientID%>').find(selector + " input:checkbox").each(function () 
    {                    
          $(this).prop("checked", $(chk).prop("checked"));     
     }); 
}  

<asp:CheckBox ID="chkAll" runat="server" onclick="SelectApproveAllCheckboxes(this, '.approve)" />     
<asp:CheckBox ID="chkAll1" runat="server" onclick="SelectRejectAllCheckboxes(this, '.reject)" />

updated:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="chkbox_checked_uncheked.aspx.cs"
    Inherits="Ediable_Repeater.chkbox_checked_uncheked" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>

    <script type="text/javascript">

        $(document).ready(function () {
            $('#C1All').click(function () {
                debugger
                $('.col1').attr("checked", $('#C1All').attr("checked"));
                $('.col2').removeAttr("checked");
                $('#C2All').removeAttr("checked");
            });

            $('#C2All').click(function () {
                debugger
                $('.col2').attr("checked", $('#C2All').attr("checked"));
                $('.col1').removeAttr("checked");
                $('#C1All').removeAttr("checked");
            });

            $('.col1').each(function () {
                $(this).click(function () {
                    var id = $(this).attr('id');
                    debugger
                    var coresId = id.replace('C1', 'C2');
                    $('#' + coresId).removeAttr("checked");
                    $('#C1All').removeAttr("checked");
                    $('#C2All').removeAttr("checked");
                });
            });

            $('.col2').each(function () {
                $(this).click(function () {
                    var id = $(this).attr('id');
                    var coresId = id.replace('C2', 'C1');
                    debugger
                    $('#' + coresId).removeAttr("checked");
                    $('#C1All').removeAttr("checked");
                    $('#C2All').removeAttr("checked");
                });
            });
        });
  </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            <table border="1">
                <tr>
                    <td>
                        <asp:CheckBox ID="C1All" runat="server" class="col1" Text="approve all" />
                    </td>
                    <td>
                        <asp:CheckBox ID="C2All" runat="server"  class="col2" Text="Reject all" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:CheckBox ID="C101" runat="server" class="col1" Text="john 0" />
                    </td>
                    <td>
                        <asp:CheckBox ID="C201" runat="server"  class="col2" Text="john 0" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:CheckBox ID="C102" runat="server" class="col1" Text="john 1" />
                    </td>
                    <td>
                        <asp:CheckBox ID="C202" runat="server"  class="col2" Text="john all" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:CheckBox ID="C103" runat="server" class="col1" Text="john 2" />
                    </td>
                    <td>
                        <asp:CheckBox ID="C203" runat="server"  class="col2" Text="John 2" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    </form>
</body>
</html>

i have a two columns (approve all/reject all) how can i restrict the user to allow only one checkbox of each?

here is the output of the screen:

i have tried something like this but did not work:

 function SelectApproveAllCheckboxes(chk, selector) 
    {     
         $('#<%=gv.ClientID%>').find(selector + " input:checkbox").each(function () 
        {                    
              $(this).prop("checked", $(chk).prop("checked"));     
         }); 
    } 
function SelectRejectAllCheckboxes(chk, selector) 
{     
     $('#<%=gv.ClientID%>').find(selector + " input:checkbox").each(function () 
    {                    
          $(this).prop("checked", $(chk).prop("checked"));     
     }); 
}  

<asp:CheckBox ID="chkAll" runat="server" onclick="SelectApproveAllCheckboxes(this, '.approve)" />     
<asp:CheckBox ID="chkAll1" runat="server" onclick="SelectRejectAllCheckboxes(this, '.reject)" />
Share Improve this question edited May 12, 2012 at 2:56 Nick Kahn asked May 12, 2012 at 0:11 Nick KahnNick Kahn 20.1k97 gold badges286 silver badges416 bronze badges 6
  • 1 I knew it was an image... but couldn't resist trying to click a checkbox. – goat Commented May 12, 2012 at 0:14
  • 2 Is it out of the question to use radio buttons? – Sam Dufel Commented May 12, 2012 at 0:14
  • 1 i update my question, yes but client would like to have checkbox and i have already thought of using radio button – Nick Kahn Commented May 12, 2012 at 0:17
  • just curious; why do you need the select all option on top of each column if you are to restrict one option from each column? – Prashanth Thurairatnam Commented May 12, 2012 at 0:18
  • i might have 100 rows + pages so user dont have to go one by one to approve or reject .... and for example i just create 4 rows – Nick Kahn Commented May 12, 2012 at 0:20
 |  Show 1 more ment

5 Answers 5

Reset to default 4

dont use checkboxes use radio buttons instead, they are designed for this.

OK based on your answer to my questions on the ments section to your question, here is a working solution. I've used a HTML table with static controls; but you should be able to apply the concepts.

<html xmlns="http://www.w3/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table border="1">
            <tr>
                <td><input type="checkbox" id="C1All" />Approve All</td>
                <td><input type="checkbox" id="C2All" />Reject All</td>
            </tr>
            <tr>
                <td><input type="checkbox" id="C101" class="col1" />John 0</td>
                <td><input type="checkbox" id="C201" class="col2" />John 0</td>
            </tr>
            <tr>
                <td><input type="checkbox" id="C102" class="col1" />John 1</td>
                <td><input type="checkbox" id="C202" class="col2" />John 1</td>
            </tr>
            <tr>
                <td><input type="checkbox" id="C103" class="col1" />John 2</td>
                <td><input type="checkbox" id="C203" class="col2" />John 2</td>
            </tr>
        </table>
    </div>
    </form>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#C1All').click(function () {
                $('.col1').attr("checked", $('#C1All').attr("checked"));
                $('.col2').removeAttr("checked");
                $('#C2All').removeAttr("checked");
            });

            $('#C2All').click(function () {
                $('.col2').attr("checked", $('#C2All').attr("checked"));
                $('.col1').removeAttr("checked");
                $('#C1All').removeAttr("checked");
            });

            $('.col1').each(function () {
                $(this).click(function () {
                    var id = $(this).attr('id');
                    var coresId = id.replace('C1', 'C2');
                    $('#' + coresId).removeAttr("checked");
                    $('#C1All').removeAttr("checked");
                    $('#C2All').removeAttr("checked");
                });
            });

            $('.col2').each(function () {
                $(this).click(function () {
                    var id = $(this).attr('id');
                    var coresId = id.replace('C2', 'C1');
                    $('#' + coresId).removeAttr("checked");
                    $('#C1All').removeAttr("checked");
                    $('#C2All').removeAttr("checked");
                });
            });
        });
    </script>
</body>

</html>

EDITED

since in asp checkboxes are nested within the span tags make use of this jquery instead of the previous one.

       $(document).ready(function () {
            $('#C1All').click(function () {
                $('.col1 > input').attr("checked", $('#C1All').attr("checked"));
                $('.col2 > input').removeAttr("checked");
                $('#C2All').removeAttr("checked");
            });

            $('#C2All').click(function () {
                $('.col2 > input').attr("checked", $('#C2All').attr("checked"));
                $('.col1 > input').removeAttr("checked");
                $('#C1All').removeAttr("checked");
            });

            $('.col1').each(function () {
                $(this).click(function () {
                    var id = $("input", this).attr('id');
                    var coresId = id.replace('C1', 'C2');
                    $('#' + coresId).removeAttr("checked");
                    $('#C1All').removeAttr("checked");
                    $('#C2All').removeAttr("checked");
                });
            });

            $('.col2').each(function () {
                $(this).click(function () {
                    var id = $("input", this).attr('id');
                    var coresId = id.replace('C2', 'C1');
                    $('#' + coresId).removeAttr("checked");
                    $('#C1All').removeAttr("checked");
                    $('#C2All').removeAttr("checked");
                });
            });
        });

I agree with using radio buttons. You can still have a "Select All" checkbox, and could have it disable the radio buttons when checked.

If you must use check boxes, you could simply overwrite the default click (check) action to first uncheck all of the other boxes. It should be pretty basic javascript with or without a library.

Try this:

$('input:checkbox').click(function(){
   $('input:checked').removeAttr('checked');
   $(this).attr('checked', 'checked');
});

That should do the trick.

If you use radio button prolly you won't get the select all feature.

You can give all of the checkboxes in the group the same class and do something like this to uncheck them all using JQuery, while checking or unchecking the one that was clicked:

$('.checkbox').click(function{
// save the original checked state of the one we're clicking:
 var checked = $(this).attr('checked'); 
 $('.checkbox').attr('checked', false); // uncheck all of the boxes
 $(this).attr('checked', !checked); // invert the original state
});

But the better choice would probably be to use a radio button control with a "none of the above" option. That way you can do validation (did that answer the question at all?)

发布评论

评论列表(0)

  1. 暂无评论