我正在使用一个简单的脚本来使用 jquery 添加和删除 div。但这不起作用。 是否需要包含任何文件?谁能帮我。
这是我的代码
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script language="javascript">
$(function() {
var scntDiv = $('#p_scents');
var i = $('#p_scents p').size() + 1;
$('#addScnt').live('click', function() {
$('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20"
name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
i++;
return false;
});
$('#remScnt').live('click', function() {
if( i > 2 ) {
$(this).parents('p').remove();
i--;
}
return false;
});
});
</script>
</head>
<body>
<h2><a href="#" id="addScnt">Add Another Input Box</a></h2>
<div id="p_scents">
<p>
<label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" /></label>
</p>
</div>
</body>
</html>
最佳答案
.live()
从 jquery 1.7 开始已弃用。在 jquery 中使用 .on()
代替 .live()
关于javascript - 使用 jquery 添加和删除 div 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24300777/