如何實現在客戶端,使CheckBox按照選擇的順序進行排序--來自博問的問題

原問題:如何實現在客戶端,使CheckBox按照選擇的順序進行排序

在客戶端,對CheckBox按照選擇的順序進行排序。
選擇多CheckBox後,如果取消其中一個CheckBox(A),則
則序號比CheckBox(A)大的均要減1,
希望的效果如下:
丁學是個好人,哈哈哈哈

解決代碼如下:(checkbox與對應的textbox要擁有相似的名字,如ckbAAA必須對應txtAAA,不然要繞圈通過parent取,懶得作,先這樣)

<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>丁學-博問demo</title>
</head>
<body>
    <form id="form1" runat="server">
    <table id="tableSn">
        <tr>
            <td><asp:CheckBox ID="ckbCarType" runat="server" Text="機型" onclick="chk(this)" /></td>
            <td><asp:TextBox ID="txtCarType" runat="server" Width="20px"></asp:TextBox></td>
            <td><asp:CheckBox ID="ckbGood" runat="server" Text="零件號" onclick="chk(this)" /></td>
            <td style="width: 20px"><asp:TextBox ID="txtGood" runat="server" Width="20px"></asp:TextBox></td>
        </tr>
        <tr>
            <td><asp:CheckBox ID="ckbLine" runat="server" Text="品別" onclick="chk(this)" /></td>
            <td><asp:TextBox ID="txtLine" runat="server" Width="20px"></asp:TextBox></td>
            <td><asp:CheckBox ID="ckbClass" runat="server" Text="班別" onclick="chk(this)" /></td>
            <td style="width: 20px"><asp:TextBox ID="txtClass" runat="server" Width="20px"></asp:TextBox></td>
        </tr>
        <tr>
            <td><asp:CheckBox ID="ckbUnContent" runat="server" Text="不良說明" onclick="chk(this)" /></td>
            <td><asp:TextBox ID="txtUnContent" runat="server" Width="20px"></asp:TextBox></td>
            <td><asp:CheckBox ID="ckbMachine" runat="server" Text="機器號" onclick="chk(this)" /></td>
            <td style="width: 20px"><asp:TextBox ID="txtMachine" runat="server" Width="20px"></asp:TextBox></td>
        </tr>
        <tr>
            <td><asp:CheckBox ID="ckbUnPlace" runat="server" Text="不良點" onclick="chk(this)" /></td>
            <td><asp:TextBox ID="txtUnplace" runat="server" Width="20px"></asp:TextBox></td>
            <td><asp:CheckBox ID="ckbPartItem" runat="server" Text="成品名" onclick="chk(this)" /></td>
            <td style="width: 20px"><asp:TextBox ID="txtPartItem" runat="server" Width="20px"></asp:TextBox></td>
        </tr>
        <tr>
            <td><asp:CheckBox ID="ckbManagePoint" runat="server" Text="部位名" onclick="chk(this)" /></td>
            <td><asp:TextBox ID="txtManagePoint" runat="server" Width="20px"></asp:TextBox></td>
            <td></td>
            <td style="width: 20px"></td>
        </tr>
        <tr>
            <td colspan="2"><asp:CheckBox ID="ckbUnDate" runat="server" Text="發生日" onclick="chk(this)" /></td>
            <td colspan="2">
                <asp:TextBox ID="txtUnDate" runat="server" Width="20px"></asp:TextBox>
                <asp:DropDownList ID="dpdDateSquence" runat="server">
                    <asp:ListItem>按月</asp:ListItem>
                    <asp:ListItem>按日</asp:ListItem>
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td colspan="2"><asp:CheckBox ID="ckbUnReason" runat="server" Text="不合格原因" onclick="chk(this)" /></td>
            <td colspan="2">
                <asp:TextBox ID="txtUnReason" runat="server" Width="20px"></asp:TextBox>
                <asp:DropDownList ID="dpdProject" runat="server"></asp:DropDownList>
            </td>
        </tr>
    </table>
    <script type="text/javascript">
        // 丁學, Ding Xue, http://www.cnblogs.com/DingXue
        var checkedIds = "";    //記錄被選中的ID們
        function chk(obj){
            if(obj.checked==true)
                checkedIds += obj.id + ",";
            else{
                document.getElementById(obj.id.replace("ckb","txt")).value="";
                checkedIds = checkedIds.replace(obj.id+",","");
            }
            var item = checkedIds.split(',');
            for(var i=0;i<item.length-1;i++)
                document.getElementById(item[i].replace("ckb","txt")).value=i+1;
        }
    </script>
    </form>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章