0
我的問題很簡單。這是我的代碼,我發現它們是兩個類似的,我該如何改進/縮短這段代碼?如何改進/縮短這2個類似的Ajax請求?
我發現它們在許多方面都很相似,這就是爲什麼我在這裏問。
這是我的代碼和任何幫助表示讚賞。
$(".follow-link").click(function(event) {
event.preventDefault();
var therel = $(this).attr('rel');
var followID = $(this).attr('rel').replace(/[^0-9]/g, '');
var thisfollow = $(this);
$.ajax({
url: '/ajax/follow.php',
type: 'POST',
data: {followwho : followID},
dataType: 'json',
success: function(data){
if (data.status) {
$('a[rel="' + therel + '"]').hide();
$('a[rel="' + therel + '"]').parent().children('.unfollow-link').fadeIn();
}
}
});
});
$(".unfollow-link").click(function(event) {
event.preventDefault();
var therel = $(this).attr('rel');
var followID = $(this).attr('rel').replace(/[^0-9]/g, '');
var thisfollow = $(this);
$.ajax({
url: '/ajax/unfollow.php',
type: 'POST',
data: {followwho : followID},
dataType: 'json',
success: function(data){
if (data.status) {
$('a[rel="' + therel + '"]').hide();
$('a[rel="' + therel + '"]').parent().children('.follow-link').fadeIn();
}
}
});
});
你也應該把url作爲參數。你有它編碼爲'/ajax/unfollow.php' – Steve
@Steve - 好點 - 我沒有看到這種差異。固定。 – jfriend00