JS如何添加行和删除行?

JS如何添加行和删除行?,第1张

<!DOCTYPE html>

<html lang="">

<head>

<meta charset="gb2312">

<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">

<title>123</title>

<script src="Scripts/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">

$(document).ready(function(){

var spotMax = 30

if($('div.spot').size() >= spotMax) {$(obj).hide()}

$("input#add").click(function(){     addSpot(this, spotMax)

})

})

function addSpot(obj, sm) {

$('div#spots').append(

'<div>' +

'<span><input name="shengfen" type="text" /></span>'+

'<span><input name="diqu" type="text" /></span>'+

'<input type="button" class="remove spot01" value="删除行" /></div>')

.find("input.remove").click(function(){

$(this).parent().remove()

$('input#add').show()

})

if($('div.spot').size() >= sm) {$(obj).hide()}

}

</script>

</head>

<body>

<span><input name="shengfen" type="text" /></span>

<span><input name="diqu" type="text" /></span>

<input type="submit" id="add" name="Submit" value="添加行" >

<div>

<form method="post" name="asdf" id="asdf"><div id="spots"></div></form>

</div>

</body>

</html>

效果这样(样式再自己调)

要加jQuery插件进去

不懂再追问

你想要的效果是,表格的每一行后面有个“添加”按钮,点击按钮后,会自动复制当前行,添加到当前行的后面,这个效果吧?

假设,我们有这样一个table表格:

<table id="table">

    <tr>

        <td>内容1</td>

        <td>

            <button class="addtr">添加</button>

            <button class="deltr">删除</button>

        </td>

    <tr>

</table>

用jquery来实现“添加”和“删除”的功能:

下面这个是添加:

$("#table").on("click","button.addtr",function(){

    var $tr=$(this).parents("tr")

    $tr.append($tr.clone())

})

下面是删除:

$("#table").on("click","button.deltr",function(){

    var $tr=$(this).parents("tr")

    $tr.remove()

})

可以参考这里,有完整的在线实例演示:

query实例:表格里点击按钮后添加一行,新添加的行附带删除按钮


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11763766.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-18
下一篇2023-05-18

发表评论

登录后才能评论

评论列表(0条)

    保存