Javascript jquery中的活动大小 $(函数(){ var scntDiv=$('p#u scents'); 变量i=$('#p#p').size()+1; $('#addScnt').live('click',function()){ $('p>')。附录(scntDiv); i++; 警觉的( $(“#测试>输入”).size() ); 返回false; }); $('#remScnt').live('click',function()){ 如果(i>2){ $(this.parents('p').remove(); 我--; } 返回false; }); });

Javascript jquery中的活动大小 $(函数(){ var scntDiv=$('p#u scents'); 变量i=$('#p#p').size()+1; $('#addScnt').live('click',function()){ $('p>')。附录(scntDiv); i++; 警觉的( $(“#测试>输入”).size() ); 返回false; }); $('#remScnt').live('click',function()){ 如果(i>2){ $(this.parents('p').remove(); 我--; } 返回false; }); });,javascript,html,jquery,Javascript,Html,Jquery,如果单击,则值=2。这很好。并添加了新的输入,但如果我再次单击,则值agan=2,应为=3。我不能用1美元。如何修改已选中live的函数size() 实例:您仍然得到2,因为您在#test段落之外追加了新输入。更新您的size()调用以检查正确的容器,方法是更换: <h2><a href="#" id="addScnt">Add Another Input Box</a></h2> <div id="p_scents"> &

如果单击,则值=2。这很好。并添加了新的输入,但如果我再次单击,则值agan=2,应为=3。我不能用1美元。如何修改已选中live的函数size()


实例:

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

<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,因为您在
#test
段落之外追加了新输入。更新您的
size()
调用以检查正确的容器,方法是更换:

<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;
        });
});


顺便说一句:“.size()方法在功能上等同于.length属性;但是,.length属性是首选的,因为它没有函数调用的开销。”方法在功能上等同于.length属性;但是,.length属性是首选的,因为它没有函数调用的开销。“它不起作用,因为您的警报语句中有一个分号-只需删除它,就像我在上面的编辑中所做的那样(这里也是:)它不起作用,因为您的警报语句中有一个分号-只需删除它,就像我在上面的编辑中所做的那样(这里也是:)
scntDiv.find('input').size()