每个人都想使用一个变量由于某种原因。这不是必需的。
$('.each_button').click(function(){
$.ajax({
context: this, // <-- do this instead...
type: 'POST',
url: process.php,
data: data,
success: function(data) {
// ...now 'this' is the element you want
alert(this.className);
}
});
});
或者使用$.proxy
如果你喜欢...
$('.each_button').click(function(){
$.ajax({
type: 'POST',
url: process.php,
data: data,
success: $.proxy(function(data) {
// ...now 'this' is the element you want
alert(this.className);
}, this) // <-- bind the context
});
});
一个好处这些方法是,它可以让你重用success
功能...
function ajax_success(data) {
alert(this.className);
}
$('.each_button').click(function(){
$.ajax({
context: this,
type: 'POST',
url: process.php,
data: data,
success: ajax_success
});
});
argh ..没有看到它的建议..我可以'关闭它,现在有人回答了 – eric01 2012-08-09 22:29:36
只是标记了我自己。谢谢你让我知道。问候 – eric01 2012-08-09 22:32:04