Jquery:Selector找不到类? [英] Jquery: Selector can't find class?

查看:55
本文介绍了Jquery:Selector找不到类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试推进Jquery-autcomplete功能。我希望Jquery-autocomplete在表中创建新行。这到目前为止工作。但我希望Jquery添加一个删除按钮。因此,用户可以删除其中一个添加的项目。

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!");
   });
    });

但是jQuery不会识别它创建的元素的点击。为了检查fpr erros,我在html中放了一行。这个按钮有效。但是当我通过Jquery添加一行时,添加的按钮不起作用。这里是一个来自firebug的例子:

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>

任何想法?

谢谢!请忘记我的英语:)

Thank you! Exuse my english :)

推荐答案

使用 jQuery live()

$('.Entf').live('click',function(event){
     alert("Thanks for visiting!");
   });

使用live使jQuery在javascript动态创建的选择器上使用回调

using live enables jQuery use callbacks on selectors that are dynamically created by javascript

这篇关于Jquery:Selector找不到类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆