我有按鈕。點擊此按鈕發送$.post()
請求並添加項目到購物籃。我想,如果用戶點擊按鈕快十倍,它不能發送10個請求,但只有一個寬度計數10 這樣的代碼:jquery優化ajax請求? php和js
<div class='qty'>
<input type='text' id="qty" value='1'>
<a class='button' href='#'>add To Basket</a>
</div>
腳本:
$(document).ready(function(){
$(".button").click(function(){
var qty=$(this).parent().find(input[type='text']).val();
$.post('/ajax.php', "qty="+qty, function(){
$(this).parent().find(input[type='text']).val("1");
});
});
});
請縮進! – Bergi
不要自己構建查詢字符串,將對象傳遞給jQuery並讓它爲您執行。 – Brad
假設你將有多個'qty'輸入,你不能爲它們中的每一個使用相同的'id'。 – josh3736