每個人都想使用一個變量由於某種原因。這不是必需的。
$('.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