gpt4 book ai didi

javascript - 为什么触发和点击在这种情况下不起作用?

转载 作者:行者123 更新时间:2023-11-29 22:03:49 25 4
gpt4 key购买 nike

我有一段代码可以:

$('td.unique').live('click', function () {  
//function logic here
});

这在我点击表格的 td 时效果很好。一切顺利!
现在我希望能够在某些情况下以编程方式拥有相同的功能,而无需用户实际按下点击。
我试过:

$(document).ready(function() {                                                                                                                                              

$(".clearButton").click( function () {
var username = $(this).closest('tr').find('input[type="hidden"][name="uname"]').val();
var user_id = $(this).closest('tr').find('label').val();

var input = [];
input[0] = {action:'reset', id:user_id,user:username,};
$.ajax({
url: 'updateprofile.html',
data:{'user_options':JSON.stringify(input)},
type: 'POST',
dataType: 'json',
success: function (res) {
if (res.status >= 1) {
//all ok
console.log("ALL OK");
$(this).closest('tr').find('.unique').trigger('click');
$(this).closest('tr').find('td.unique').trigger('click');
$(this).closest('tr').find('td.unique').click();


}
else {
alert('failed');
}
}
});

此按钮与 td.unique 位于同一行

这些都不起作用。为什么?我做错了吗?我在live中绑定(bind)的函数是不是按这种方式不考虑?

最佳答案

您需要在 ajax 函数中缓存 $(this)。

var $this = $(this);

ajax 函数中的 $(this) 不会引用被点击的元素

$(".clearButton").click(function () {
var $this = $(this);
var username = $this.closest('tr').find('input[type="hidden"][name="uname"]').val();
var user_id = $this.closest('tr').find('label').val();
var input = [];

input[0] = {
action: 'reset',
id: user_id,
user: username,
};

$.ajax({
url: 'updateprofile.html',
data: {
'user_options': JSON.stringify(input)
},
type: 'POST',
dataType: 'json',
success: function (res) {
if (res.status >= 1) {
console.log("ALL OK");
$this.closest('tr').find('.unique').trigger('click');
$this.closest('tr').find('td.unique').trigger('click');
$this.closest('tr').find('td.unique').click();
} else {
alert('failed');
}
}
});
});

关于javascript - 为什么触发和点击在这种情况下不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22071691/

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