首次运行函数后丢失jQuery行为

时间:2012-01-15 08:28:26

标签: jquery function

我有一个表格,里面有一些行和输入。每当用户在文本框中按 + 键时,我想在其字段中添加一个新行。我能做到,但我遇到的问题是这个功能第一次运行。如果我在第二个字段上按 + 键,则不会发生任何事情。

This is my code in jsFiddle

$(document).ready(function(){
    $(".data").bind('keyup',function(e){
        if(e.which == 107){
            var id = $(this).attr('id');
            var content = $('#'+id).val();
            var newContent = content.replace('+','');
            $('#'+id).val(newContent);

            var current_rows = ($('#myTable tr').length);
            current_rows = parseInt(current_rows);
            var newId = current_rows + 1;

            var newRow = '<td><input type="text" class="data" id="'+newId+'" /></td>';
            $('#myTable tr:last').after(newRow);
            $('#'+newId).focus();
        }
    });
    $('#1').focus();
});

我必须做什么?

1 个答案:

答案 0 :(得分:2)

尝试使用live而不是bind。但是看一下文档,我认为现在已经过时了。