jquery动态添加行, table checkbox全选

jquery动态添加行及获取值

<head>
    <title>table动态添加行</title>
    <script src="Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
</head>
<body>
    <table id="table" border="0" style="text-align:center">
        <tr><td>id</td><td>one</td><td>two</td></tr>
        <tr class="tr">
            <td><input type="text" class="bianhao" value="1" disabled="disabled" style="width:15px;" /></td>
            <td><input type="text" class="one" /></td>
            <td><input type="text" class="two" /></td>
        </tr>
    </table>
    <input type="button" value="添加" οnclick="AddTr();" />&nbsp;&nbsp;
    <input type="button" value="得到值" id="btnGetValue"/>
    <div id="ShowValue"></div>
<script type="text/javascript">
    function AddTr() {
        var i = $('#table tr:gt(0)').length;
        if (i >= 5) {
            alert('最多有5行');
            return;
        }
        var table = $('#table');
        var tr_1 = $('<tr class="tr"></tr>');
        var td_bianhao = $('<td><input type="text" class="bianhao" disabled="disabled" value="' + (i + 1) + '" style="width:15px;" /></td>');
        var td_one = $('<td><input type="text" class="one" /></td>');
        var td_two = $('<td><input type="text" class="two" /></td>');
        var td_delete = $('<td><a href="javascript:void(0);">删除</a></td>');
        td_delete.click(function () {
            if (confirm('是否删除')) {
                tr_1.remove();
            }
            $('.bianhao').each(function (i, item) {
                item.value = (i + 1);
            });
        });
        tr_1.append(td_bianhao);
        tr_1.append(td_one);
        tr_1.append(td_two);
        tr_1.append(td_delete);
        tr_1.appendTo(table);
    }

    function checkForm() {
        if (!isNullOne()) {
            alert('one列不能为空');
            return false;
        }
        if (!isNullTwo()) {
            alert('two为1到3位数字');
            return false;
        }
        var values = "";
        $('.tr').each(function (i, item) {
            var id = $(item).find(".bianhao").val();
            var one = $(item).find(".one").val();
            var two = $(item).find(".two").val();
            var str = id + "/" + one + "/" + two + ",<br/>";
            values += str;
        });
        $('#ShowValue').html(values);
    }

    function isNullOne() {
        var t = true;
        $('.one').each(function (i, item) {
            if ($(item).val() == "") {
                t = false;
            }
        });
        return t;
    }

    function isNullTwo() {
        var t = true;
        var reg = /^\d{1,3}$/;
        $('.two').each(function (i, item) {
            if (!reg.test($(item).val())) {
                t = false;
            }
        });
        return t;
    }

    $('#btnGetValue').click(function () {
        return checkForm();
    });
</script>
</body>

table 表里checkbox元素全选

 
<body>
    <table id="table" border="0">
        <tr><td colspan="2"><input type="checkbox" value="全选" οnclick="CheckAll('table',this)" /></td></tr>
        <tr><td><input type="checkbox" /></td><td>111</td></tr>
        <tr><td><input type="checkbox" /></td><td>222</td></tr>
        <tr><td><input type="checkbox" /></td><td>333</td></tr>
        <tr><td><input type="checkbox" /></td><td>444</td></tr>
        <tr><td><input type="checkbox" /></td><td>555</td></tr>
    </table>
<script type="text/javascript">
    function CheckAll(table, check) {
        $('#' + table + " :checkbox:gt(0)").each(function (i, item) {
            item.checked = check.checked;
        });
    }
</script>
</body>
 
如果有不懂的地方,请加群:217827517.  进行咨询. 

转载于:https://www.cnblogs.com/vsod/archive/2012/09/25/2701886.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值