gpt4 book ai didi

javascript - jquery 中的实时大小

转载 作者:行者123 更新时间:2023-11-28 21:19:53 25 4
gpt4 key购买 nike

<h2><a href="#" id="addScnt">Add Another Input Box</a></h2>  <div id="p_scents">     
<p id="test">
<input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" />
<input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" />
</p> </div>


$(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++;

alert(

$("#test > input").size()

);

return false;
});

$('#remScnt').live('click', function() {
if( i > 2 ) {
$(this).parents('p').remove();
i--;
}
return false;
});
});

如果我点击,则值 = 2。这很好。并添加了新的输入,但如果我再次单击,则值 agan = 2,应该是 = 3。我不能使用 $i。我如何修改实时检查的函数size()

实例:http://jsfiddle.net/u9uhV/6/

最佳答案

您仍然得到 2,因为您已将新输入附加到 #test 段落的外部。更新您的 size() 调用以检查正确的容器,方法是替换:

$("#test > input").size()

scntDiv.find('input').size()

关于javascript - jquery 中的实时大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6655092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com