小编Joh*_*ods的帖子

Javascript将行添加到HTML表和增量ID

这是我在这个网站上的第一篇文章,所以希望你能对我很轻松.我正在尝试创建一个HTML/PHP表单,并在单击一个按钮时使用一小段Javascript向表中添加其他行,并增加两个字段的ID.

该按钮用于添加行,但它似乎不会增加ID,只需使用与上一行相同的ID.希望有人可以帮忙吗?

$(window).load(function(){
        var table = $('#productanddates')[0];

    var newIDSuffix = 2;
    $(table).delegate('#button2', 'click', function () {
        var thisRow = $(this).closest('tr')[0];

        var cloned = $(thisRow).clone();
        cloned.find('input, select').each(function () {
            var id = $(this).attr('id');
            id = id.substring(0, id.length - 1) + newIDSuffix;
            $(this).attr('id', id);
        });

        cloned.insertAfter(thisRow).find('input:date').val('');

        newIDSuffix++;
    });
});  
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="blue-bar ta-l">
    <div class="container">
        <h1>Submit Your Insurance Renewal Date(s)</h1>
    </div>
</div>
<div class="grey-bar">
    <div class="container">
        <div class="rounded-box">
            <div>
                <label for="name">Name</label>
                <input type="text" id="name" name="name" autocomplete="off" required />
            </div>
            <div>
                <label …
Run Code Online (Sandbox Code Playgroud)

html javascript

6
推荐指数
1
解决办法
2452
查看次数

标签 统计

html ×1

javascript ×1