如何实现在table中 用append添加的行 自动设置不同的name?

前端的js代码:

$(document).ready(function () {

                $('.a1').click(function () {

                    var b1 = $('.b1').val();

                    var b2 = $('.b2').val();

                    var newRow = "<tr><td>" + b1 + "</td><td>" + b2 + "</td></tr>";

                    $(".c1").append(newRow);

                });

            });

            

            

我通过append实现了添加行的操作, 但是我想给每个新添加的行赋一个特定不同的的name(比如说我添加了三行,这三个tr的name分别为1 2 3),然后把带有name的行传到后端,请问怎么才能实现这个功能呢?

烙印99
浏览 451回答 1
1回答

慕码人8056858

var flag = 0;&nbsp;$('.a1').click(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var b1 = $('.b1').val();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var b2 = $('.b2').val();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newRow = "<tr><td>" + b1 + "</td><td>" + b2 + "</td></tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(".c1").append(newRow);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(".c1").addClass(flag);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript