Jquery:选择器找不到类?
我正在尝试推进 Jquery-autcomplete 功能。我希望 Jquery 自动完成在表中创建新行。到目前为止,这有效。但我希望 Jquery 添加一个删除按钮。因此用户可以删除他添加的项目之一。
$(document).ready(
function() {
//create an new <tr> <td> in #log
function log( message ) {
$("<tr> <td>" + message + "<input class='Entf' type='button' value ='Entfernen' />" + "</td></tr>").appendTo("#log");
$( "#log" ).attr( "scrollTop", 0 );}
// get the values from a json-source
$( "#REMOTE" ).autocomplete({
source: "json.php",
minLength: 0,
select: function( event, ui ) {
log( ui.item ?
ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );}
});
// this is just a test if jquery recognizes the click
$('.Entf').click(function(event){
alert("Thanks for visiting!");
});
});
但 jQuery 无法识别它创建的元素的单击。为了检查 fpr 错误,我在 html 中放置了一行。这个按钮有效。但是当我通过 Jquery 添加行时,添加的按钮不起作用。这是 firebug 的一个例子:
<table id="log" border="0" width="400">
<tbody>
<tr>
<td>
test
<input class="Entf" type="button" value="Entfernen"> //this one workes fine. it comes from the original html
</td>
</tr>
<tr>
<td>
daniel aka 121
<input class="Entf" type="button" value="Entfernen"> //this one doesn't work. it's the one that was added by Jquery
</td>
</tr>
</tbody>
</table>
有什么想法吗?
谢谢你!请原谅我的英语:)
I'm trying to advance the Jquery-autcomplete function. I want Jquery-autocomplete to create new rows in a table. That works so far. But I want Jquery to add a delete-button. So the user is able to delete one of his added items.
$(document).ready(
function() {
//create an new <tr> <td> in #log
function log( message ) {
$("<tr> <td>" + message + "<input class='Entf' type='button' value ='Entfernen' />" + "</td></tr>").appendTo("#log");
$( "#log" ).attr( "scrollTop", 0 );}
// get the values from a json-source
$( "#REMOTE" ).autocomplete({
source: "json.php",
minLength: 0,
select: function( event, ui ) {
log( ui.item ?
ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );}
});
// this is just a test if jquery recognizes the click
$('.Entf').click(function(event){
alert("Thanks for visiting!");
});
});
But jQuery doesen't recognize the click of the element it created. To check fpr erros, I placed one row in the html. This button works. But when I add a row via Jquery, the added button deon't work. Here an example from firebug:
<table id="log" border="0" width="400">
<tbody>
<tr>
<td>
test
<input class="Entf" type="button" value="Entfernen"> //this one workes fine. it comes from the original html
</td>
</tr>
<tr>
<td>
daniel aka 121
<input class="Entf" type="button" value="Entfernen"> //this one doesn't work. it's the one that was added by Jquery
</td>
</tr>
</tbody>
</table>
Any ideas?
Thank you! Exuse my english :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
use jQuery live():
使用 live 使 jQuery 在由 javascript 动态创建的选择器上使用回调
use jQuery live():
using live enables jQuery use callbacks on selectors that are dynamically created by javascript
因为该元素是在绑定处理程序后创建的,所以它没有注册。你需要这样的东西:
Because the element is getting created after the handlers are bound, it's not registering. You need something like this:
在 jQuery 1.7+ 中使用 jQuery on()
使用 jQuery delegate() 在 jQuery 1.4.3+ 中:
并在 jQuery 1.3 中使用 jQuery live():
Use jQuery on() in jQuery 1.7+
Use jQuery delegate() in jQuery 1.4.3+:
And use jQuery live() in jQuery 1.3:
生成内容后,需要绑定按钮,即生成内容后,
再次执行:。
编辑:正如尼尔所指出的,使用这种方法时要小心。原因是它将绑定到所有 .Entf 元素,而不仅仅是新元素,从而导致多个单击事件。有关如何创建和绑定的示例,请参阅 http://jsfiddle.net/R9Kb4/2/无需处理 live() 或多重绑定。
When you have generated content, you need to bind the button, ie, after you have generated the content, do:
again.
Edit: As pointed out by Neal, be careful when doing it this method. The reason as that it will bind to ALL .Entf elements, not just the new ones, causing multiple click events. See http://jsfiddle.net/R9Kb4/2/ for an example of how to create and bind things without having to deal with live() or with multiple binding.