动态生成的文本框的按键事件

Key press event of a dynamically generated textbox

tr.append($("<td/>").html('<input type="text" name="unit_price" id="unit_price"/>'));

这是我动态生成的文本框,我已经尝试了很多类似下面的解决方案,但它不起作用帮助我极客们

$("#unit_price").live("keyup", function(){  
     //Your Code to handle the click.
    }); 

$(staticAncestors).on(eventName, dynamicChild, function() {});

您需要使用event delegation.

Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future.

$("body").on("keyup","#unit_price", function(){  
 //Your Code to handle the click.
}); 

使用这个

$(document).on("keyup","#unit_price", function(){  
 //Results here
}); 

试试这个..

<table >
    <tr>
        <td class="test"></td>  

    </tr>
</table>

$(document).ready(function(){

$(".test").append($("<td/>").html('<input type="text" name="unit_price" id="unit_price"/>'));
     $("#unit_price").keyup(function(){
alert($(this).val());
  });
});

演示:http://jsfiddle.net/cz1dvzvf/1/