在我的页面中,我需要将当前变量作为参数提供给函数AjaxLoader,然后向其中添加所选的类。但是,当我将其作为参数传递时,它不起作用。有没有办法做到这一点?我的代码如下。
$('a').click(function() {
var current = $(this).find('td');
current.addClass("selected");
AjaxLoader(current);
}
function AjaxLoader(current){
$.ajax({
type: "POST",
url: "test1.php",
success: function(response) {
//this is what I want to do
current.addClass("selected");
}
})
}
发布于 2012-03-22 20:55:15
此外,您还可以使用ajax参数的上下文成员
$.ajax( {..., context: current, success: function () { $(this) // current
请参阅http://api.jquery.com/jQuery.ajax/
发布于 2012-03-22 20:53:11
您没有关闭您的函数。此外,在使用函数之前,您还需要定义函数。
function AjaxLoader(current){
$.ajax({
type: "POST",
url: "test1.php",
success: function(response) {
//this is what I want to do
current.addClass("selected");
}
});
}
$('a').click(function() {
var current = $(this).find('td');
current.addClass("selected");
AjaxLoader(current);
});
发布于 2012-03-22 20:59:24
在A标记中有一个表是非常不可能的。您可能希望使用closest()
或parent()
而不是find()
来获取包含A标记的td。find()
查找元素的后代
http://api.jquery.com/closest/
http://api.jquery.com/parent/
http://api.jquery.com/parents/
$('a').click(function() {
var current = $(this).closest('td');
current.addClass("selected");
AjaxLoader(current);
});
function AjaxLoader(current){
$.ajax({
type: "POST",
url: "test1.php",
success: function(response) {
//this is what I want to do
current.addClass("selected");
}
});
};
https://stackoverflow.com/questions/9822570
复制相似问题