jQuery簡(jiǎn)單實(shí)現(xiàn)對(duì)表格的增刪改查

表格數(shù)據(jù).png
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <style>
        #bgDiv {
            position: absolute;
            left: 0;
            top: 0;
            background-color: black;
            opacity: 0.2; /*設(shè)置不透明度*/
            display: none;
        }

        #fgDiv {
            position: absolute;
            width: 250px;
            height: 100px;
            border: 1px solid #808080;
            background-color: #808080;
            display: none;
            border-radius: 5px 5px; /*設(shè)置圓角*/
        }

        td {
            text-align: center;
        }

        #fgDiv input {
            align-items: center;
        }
    </style>
    <script src="script/jquery-3.2.1.js"></script>
    <script>
        $(function () {

            var list = [
                { id: "1", country: "中國(guó)", capital: "北京" },
                { id: "2", country: "美國(guó)", capital: "紐約" },
                { id: "3", country: "日本", capital: "東京" },
                { id: "4", country: "韓國(guó)", capital: "首爾" },
            ];
            //生成表格數(shù)據(jù)
            $.each(list, function (index, item) {
                $('<tr id="' + item.id + '"><td><input type="checkbox" /></td><td>' + item.id + '</td><td>' + item.country + '</td><td>' + item.capital + '</td><td><input type="button" value="修改"/></td></tr>').appendTo($("#list"));
            });
            //為復(fù)選框checkAll設(shè)置點(diǎn)擊事件,完成全選脓杉、全消的功能
            $("#checkAll").click(function () {
                //根據(jù)當(dāng)前復(fù)選框的狀態(tài)設(shè)置其它行復(fù)選框的狀態(tài)
                $("#list :checkbox").attr("checked", this.checked);
            });
            //反選
            $("#btnReverse").click(function () {
                //數(shù)據(jù)行的復(fù)選框
                $("#list :checkbox").each(function () {
                    this.checked = !this.checked;
                });
            });
            //刪除選中行
            $("#btnDelete").click(function () {
                //$("#list :checked").parent().parent().remove();
                $("#list :checked").parents("tr").remove();
            });
            //添加
            $("#btnAdd").click(function () {
                //顯示添加界面
                $("#bgDiv").css("display", "block").css("width", window.innerWidth + "px").height(window.innerHeight + "px");
                $("#fgDiv").css("display", "block").css("left", (window.innerWidth - 250) / 2 + "px").css("top", (window.innerHeight - 100) / 2 + "px");

                //打開的清除文本框中的數(shù)據(jù)
                //$("#fgDiv input[type=text]").val('');
                $("#fgDiv :text,:hidden").val('');
            });
            //保存
            $("#btnSave").click(function () {
                var id = $("#hidId").val();
                if (id == '') {//添加
                    $('<tr id="' + $("#textId").val() + '"><td><input type="checkbox"/></td><td>' + $("#textId").val() + '</td><td>' + $("#textCountry").val() + '</td><td>' + $("#textCapital").val() + '</td><td><input type="button" value="修改"/></td></tr>').appendTo($("#list")).find(":button").click(function () {
                        //顯示添加界面
                        $("#bgDiv").css("display", "block").css("width", window.innerWidth + "px").height(window.innerHeight + "px");
                        $("#fgDiv").css("display", "block").css("left", (window.innerWidth - 250) / 2 + "px").css("top", (window.innerHeight - 100) / 2 + "px");
                        //找到當(dāng)前按鈕所在td之前的所有td
                        var tds = $(this).parent().prevAll();
                        //設(shè)置文本框的值
                        $("#hidId").val(tds.eq(2).text());//隱藏域存放修改之前的行的ID編號(hào)值
                        $("#textId").val(tds.eq(2).text());
                        $("#textCountry").val(tds.eq(1).text());
                        $("#textCapital").val(tds.eq(0).text());
                    });
                    //為最新添加的一行數(shù)據(jù)的修改按鈕綁定事件
                    //$("#list :button:last").click(function () {
                    //    //顯示添加界面
                    //    $("#bgDiv").css("display", "block").css("width", window.innerWidth + "px").height(window.innerHeight + "px");
                    //    $("#fgDiv").css("display", "block").css("left", (window.innerWidth - 250) / 2 + "px").css("top", (window.innerHeight - 100) / 2 + "px");
                    //    //找到當(dāng)前按鈕所在td之前的所有td
                    //    var tds = $(this).parent().prevAll();
                    //    //設(shè)置文本框的值
                    //    $("#hidId").val(tds.eq(2).text());//隱藏域存放修改之前的行的ID編號(hào)值
                    //    $("#textId").val(tds.eq(2).text());
                    //    $("#textCountry").val(tds.eq(1).text());
                    //    $("#textCapital").val(tds.eq(0).text());
                    //});
                } else {//修改
                    var trds = $("#" + id + ">td");
                    trds.eq(1).text($("#textId").val());
                    trds.eq(2).text($("#textCountry").val());
                    trds.eq(3).text($("#textCapital").val());
                    //修改tr的id屬性,保持?jǐn)?shù)據(jù)一致
                    $("#" + id).attr("id", $("#textId").val());
                }
                //隱藏界面
                $("#bgDiv").css("display", "none");
                $("#fgDiv").css("display", "none");
            });
            //修改
            $("#list :button").click(function () {
                //顯示添加界面
                $("#bgDiv").css("display", "block").css("width", window.innerWidth + "px").height(window.innerHeight + "px");
                $("#fgDiv").css("display", "block").css("left", (window.innerWidth - 250) / 2 + "px").css("top", (window.innerHeight - 100) / 2 + "px");
                //找到當(dāng)前按鈕所在td之前的所有td
                var tds = $(this).parent().prevAll();
                //設(shè)置文本框的值
                $("#hidId").val(tds.eq(2).text());//隱藏域存放修改之前的行的ID編號(hào)值
                $("#textId").val(tds.eq(2).text());
                $("#textCountry").val(tds.eq(1).text());
                $("#textCapital").val(tds.eq(0).text());
               
            })
        });
    </script>
</head>
<body>
    <input type="button" value="添加" id="btnAdd" />
    <input type="button" id="btnReverse" value="反選" />
    <input type="button" id="btnDelete" value="刪除" />
    <table border="1">
        <thead>
        <th width="100"><input type="checkbox" id="checkAll" /></th>
        <th width="200">編號(hào)</th>
        <th width="200">國(guó)家</th>
        <th width="200">首都</th>
        <th width="100">修改</th>
        </thead>
        <tbody id="list"></tbody>
    </table>
    <div id="bgDiv"></div>
    <div id="fgDiv">
        <input type="hidden" id="hidId" />
        編號(hào):<input type="text" id="textId" />
        <br>
        國(guó)家:<input type="text" id="textCountry" />
        <br>
        首都:<input type="text" id="textCapital" />
        <br>
        <input type="button" id="btnSave" value="保存" />
    </div>
</body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市觉啊,隨后出現(xiàn)的幾起案子焕梅,更是在濱河造成了極大的恐慌值漫,老刑警劉巖炼绘,帶你破解...
    沈念sama閱讀 221,430評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件椭蹄,死亡現(xiàn)場(chǎng)離奇詭異闻牡,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)绳矩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,406評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門罩润,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人翼馆,你說(shuō)我怎么就攤上這事割以。” “怎么了应媚?”我有些...
    開封第一講書人閱讀 167,834評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵严沥,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我珍特,道長(zhǎng)祝峻,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,543評(píng)論 1 296
  • 正文 為了忘掉前任扎筒,我火速辦了婚禮莱找,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘嗜桌。我一直安慰自己奥溺,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,547評(píng)論 6 397
  • 文/花漫 我一把揭開白布骨宠。 她就那樣靜靜地躺著浮定,像睡著了一般。 火紅的嫁衣襯著肌膚如雪层亿。 梳的紋絲不亂的頭發(fā)上桦卒,一...
    開封第一講書人閱讀 52,196評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音匿又,去河邊找鬼方灾。 笑死,一個(gè)胖子當(dāng)著我的面吹牛碌更,可吹牛的內(nèi)容都是我干的裕偿。 我是一名探鬼主播,決...
    沈念sama閱讀 40,776評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼痛单,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼嘿棘!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起旭绒,我...
    開封第一講書人閱讀 39,671評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤鸟妙,失蹤者是張志新(化名)和其女友劉穎焦人,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體圆仔,經(jīng)...
    沈念sama閱讀 46,221評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡垃瞧,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,303評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了坪郭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片个从。...
    茶點(diǎn)故事閱讀 40,444評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖歪沃,靈堂內(nèi)的尸體忽然破棺而出嗦锐,到底是詐尸還是另有隱情,我是刑警寧澤沪曙,帶...
    沈念sama閱讀 36,134評(píng)論 5 350
  • 正文 年R本政府宣布奕污,位于F島的核電站,受9級(jí)特大地震影響液走,放射性物質(zhì)發(fā)生泄漏碳默。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,810評(píng)論 3 333
  • 文/蒙蒙 一缘眶、第九天 我趴在偏房一處隱蔽的房頂上張望嘱根。 院中可真熱鬧,春花似錦巷懈、人聲如沸该抒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,285評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)凑保。三九已至,卻和暖如春涌攻,著一層夾襖步出監(jiān)牢的瞬間欧引,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,399評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工恳谎, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留维咸,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,837評(píng)論 3 376
  • 正文 我出身青樓惠爽,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親瞬哼。 傳聞我的和親對(duì)象是個(gè)殘疾皇子婚肆,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,455評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容