jquery-未读取文本框

jquery-未读取文本框,jquery,autosuggest,Jquery,Autosuggest,我正在尝试为多个文本框制作一个自动建议搜索框的基础。我已经有了一个脚本来添加/remvoe文本框,它工作得很好。但是我遇到的问题是,当按下一个键(基本上是onkeyup键)时,能够获取页面上的所有文本框,它会提醒文本框的值。我在html中添加了自己的文本框,但使用jquery添加的任何文本框都不起作用 以下是jquery: $(document).ready(function(){ $counter = 0; // initialize 0 for limitting textb

我正在尝试为多个文本框制作一个自动建议搜索框的基础。我已经有了一个脚本来添加/remvoe文本框,它工作得很好。但是我遇到的问题是,当按下一个键(基本上是onkeyup键)时,能够获取页面上的所有文本框,它会提醒文本框的值。我在html中添加了自己的文本框,但使用jquery添加的任何文本框都不起作用

以下是jquery:

    $(document).ready(function(){
    $counter = 0; // initialize 0 for limitting textboxes
    $('#buttonadd').click(function(){
        if ($counter < 10)
        {
            $counter++;
            $('#buttondiv').append('<div><label>Textbox #'+$counter+'</label><input type="text" name="textbox[]" class="textbox" value="" id="country"/></div>');
        }else{
            alert('You cannot add more than 10 textboxes');
        }
    });

    $('#buttonremove').click(function(){
        if ($counter){
            $counter--;
            $('#buttondiv .textbox:last').parent().remove(); // get the last textbox and parent for deleting the whole div
        }else{
            alert('No More textbox to remove');
        }
    });

    $('#buttonget').click(function(){
        alert($('.textbox').serialize()); // use serialize to get the value of textbox
    });

    $('input').bind('keyup', function() {
        alert($(this).val());
    });

    $('#dropdownadd').change(function(){
        $('#dropdowndiv').html(""); // when the dropdown change set the div to empty
        $loopcount = $(this).val(); // get the selected value
        for (var i = 1; i <= $loopcount; i++)
        {
            $('#dropdowndiv').append('<div><label>Textbox #'+i+'</label><input type="text" name="textbox2[]" class="textbox2" value="" /></div>');
        }
    });
});
$(文档).ready(函数(){
$counter=0;//为限制文本框初始化0
$(“#按钮添加”)。单击(函数(){
如果($counter<10)
{
$counter++;
$('#buttondiv').append('Textbox#'+$counter+'');
}否则{
警报(“您不能添加超过10个文本框”);
}
});
$(“#按钮删除”)。单击(函数(){
如果($柜台){
$counter--;
$('#buttondiv.textbox:last').parent().remove();//获取用于删除整个div的最后一个文本框和父对象
}否则{
警报(“不再需要删除文本框”);
}
});
$(“#按钮集”)。单击(函数(){
警报($('.textbox').serialize());//使用serialize获取textbox的值
});
$('input').bind('keyup',function(){
警报($(this.val());
});
$('#dropdownadd').change(function(){
$('#dropdowndiv').html(“”;//当下拉更改将div设置为空时
$loopcount=$(this).val();//获取所选值

对于(var i=1;i尝试使用上的
而不是
单击

这应该可以通过委派来处理动态添加的元素。我添加了一个div来包装这些元素,包括手动添加和动态添加,然后您可以应用它

$('#container').on('keyup', 'input', function() {
    alert($(this).val());
});

请参见此

之所以会发生这种情况,是因为在生成文本框之前调用了keyup的绑定函数。因此,每次创建新文本框时都要运行绑定代码(如下所示)

$('input').bind('keyup', function() {
        alert($(this).val());
    });
或者更好的方法是使用普通JavaScript onkeyup属性来调用您定义的函数。 e、 g

$('#buttondiv').append('Textbox#'+$counter+'');

使用onkeyup是我不久前尝试的第一件事,但由于某些原因,这次成功了。谢谢!
$('input').bind('keyup', function() {
        alert($(this).val());
    });
$('#buttondiv').append('<div><label>Textbox #'+$counter+'</label><input type="text" name="textbox[]" class="textbox" onkeyup="function_abc(this);" value="" id="country"/></div>');