Jquery:选择器找不到类?

发布于 2024-10-28 00:05:00 字数 1578 浏览 2 评论 0原文

我正在尝试推进 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ゝ杯具 2024-11-04 00:05:00

use jQuery live()

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

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

use jQuery live():

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

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

兔姬 2024-11-04 00:05:00

因为该元素是在绑定处理程序后创建的,所以它没有注册。你需要这样的东西:

$("body").delegate(".Entf", "click", function() { //function here });

Because the element is getting created after the handlers are bound, it's not registering. You need something like this:

$("body").delegate(".Entf", "click", function() { //function here });
腹黑女流氓 2024-11-04 00:05:00

在 jQuery 1.7+ 中使用 jQuery on()

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

使用 jQuery delegate() 在 jQuery 1.4.3+ 中:

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

并在 jQuery 1.3 中使用 jQuery live():

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

Use jQuery on() in jQuery 1.7+

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

Use jQuery delegate() in jQuery 1.4.3+:

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

And use jQuery live() in jQuery 1.3:

$('.Entf').live('click',function(event){
     alert("Thanks for visiting!");
   });
盗梦空间 2024-11-04 00:05:00

生成内容后,需要绑定按钮,即生成内容后,

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

再次执行:。

编辑:正如尼尔所指出的,使用这种方法时要小心。原因是它将绑定到所有 .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:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文