废话不多说,直接上代码
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jq实现点击添加和删除一行</title>
<script src="scripts/jquery.js" type="text/javascript"></script>
</head>
<body>
<div id="line">
<div>
<input type='button' id='add' value='加选项' />
<input type='button' id='minus' value='减选项'/>
</div>
<p><span></span><input type="text" name=""></p>
</div>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$("#add").click(function(){
var num =$("#line").children("p").length+1;
$("#line").append(("<p><span></span><input type='text' name=''></p>"));
});
$("#minus").click(function(){
var length=$("#line").children("p").length;
if(length<2){
alert("至少保留一项");
}else{
$("#line").children("p:last-child").remove();
}
});
</script>
</body>