我试图在jsfiddle中执行一个jQuery代码。我的jQuery代码如下
$(function() {
var scntDiv = $('#p_scents');
var i = $('#p_scents p').size() + 1;
$('#addScnt').on('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').on('click', function() {
alert("sfdrijg");
if( i > 2 ) {
$(this).parents('p').remove();
i--;
}
return false;
});
});
我的HTML代码如下。
<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>
我的页面看起来像这样
单击“添加另一个输入框”始终有效,而单击“删除”则完全不响应。
尝试将您的代码更改为
$(document).on('click','#remScnt', function() {
请参阅这是一个很好的答案,以获取更多详细信息。https://stackoverflow.com/a/8752376/1640577
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句