gpt4 book ai didi

javascript - Jquery:选择器找不到类?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:37:44 25 4
gpt4 key购买 nike

我正在尝试改进 Jquery 自动完成功能。我希望 Jquery-autocomplete 在表中创建新行。到目前为止有效。但我希望 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>

有什么想法吗?

谢谢!请原谅我的英语:)

最佳答案

使用jQuery live() :

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

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

关于javascript - Jquery:选择器找不到类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5477671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com