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

4
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() {});

1
"live"已被弃用。请参阅http://api.jquery.com/live/。 - haim770
3个回答

2

您需要使用事件委托

事件委托允许我们将单个事件侦听器附加到父元素,该元素将为与选择器匹配的所有后代触发,无论这些后代现在存在还是将来添加。

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

1
尝试这个:

试试看...

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

Demo:http://jsfiddle.net/cz1dvzvf/1/


0

使用这个

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接