我試圖通過鏈接點擊提交AJAX功能,我使用的onclick像這樣:的onclick正在訪問的鏈接無論返回false
<a href="<?php echo SITE_URL; ?>watchlist/remove/<?php echo System::escape($this->item->item_id); ?>" onclick="manage_watchlist(); return false;" id="watchlist">
Remove From Watchlist
</a>
和我的功能:
var manage_watchlist = function(e)
{
alert("ff");
e.preventDefault();
$.ajax({
url: $(this).attr('href'),
type: 'post',
dataType: 'json',
success: function(data)
{
if(data.success)
{
if($(this).hasClass("remove"))
{
$(this).text("Add to Watchlist");
$(this).removeClass("remove").addClass("add");
$(this).attr('href', '<?php echo SITE_URL; ?>watchlist/add/<?php echo System::escape($this->item->item_id); ?>');
}
else
{
$(this).text("Remove from Watchlist");
$(this).removeClass("add").addClass("remove");
$(this).attr('href', '<?php echo SITE_URL; ?>watchlist/remove/<?php echo System::escape($this->item->item_id); ?>');
}
$('#watch_success_message').html(data.success + alert_close).show();
}
else
{
$('#watch_error_message').html(data.error + alert_close).show();
}
<!-- hide loading icon -->
$('.ajloading').hide();
},
error: function()
{
<!-- hide loading icon -->
$('.ajloading').hide();
}
});
return false;
}
我已經在函數中使用了一個警告,所以我的函數正在工作,但由於某種原因它仍然在訪問該鏈接?
你不應該需要兩個'返回FALSE'和'e.preventDefault()'...我只是使用後建議。 – evolutionxbox
''.ajax'內部'this'是'$ .ajax'對象。 –
返回錯誤不會阻止鏈接發射。它將「只」停止傳播(當偵聽器使用jQuery進行設置時它不同,但這不是你在這裏做的)。 –