AJAX避免用戶重復(fù)提交請求實現(xiàn)方案
更新時間:2013年04月03日 16:50:45 作者:
為了避免因某些原因用戶同時多次點擊按鈕,提交重復(fù)的請求,我們需要禁用請求提交按鈕,接下來與大家一起分享下實現(xiàn)方法
在使用AJAX(jQuery)異步請求數(shù)據(jù)時,為了避免因某些原因用戶同時多次點擊按鈕,提交重復(fù)的請求,我們需要禁用請求提交按鈕。
重點:jQuery的 attr 和 removeAttr 兩個函數(shù),主要是元素button的disabled屬性。
Demo:
<body>
<a class="disabled">Button disabeld</a> <a class="abled">Button abled</a><br/><br/>
<input type="button" id="submit" name="submit" value="submit">
</body>
<script type="text/javascript" src="../jquery-1.2.6.js"></script>
<script type="text/javascript">
$(function(){
//使其失效
$(".disabled").click(function(){
$("#submit").attr("disabled","disabled");
$("#submit").val('disabled...');
});
//激活
$(".abled").click(function(){
$("#submit").removeAttr("disabled");
$("#submit").val('submit');
});
//操作請求
$("#submit").click(function() {
$("#submit").attr("disabled", "disabled");
alert("hi");//send ajax request
$("#submit").removeAttr("disabled");
});
});
</script>
說明:當點擊"Button disabeld"后,使用 $( "#submit" ).attr("disabled","disabled") 語句禁用測試按鈕,實質(zhì)是給測試按鈕增加一個disabled屬性,然后點擊"Button abled"或執(zhí)行發(fā)送ajax()請求,當請求完全后,使用 $("#submit").removeAttr("disabled"); 語句去除禁用按鈕屬性,從而實現(xiàn)了禁用按鈕,避免重復(fù)發(fā)送請求。
重點:jQuery的 attr 和 removeAttr 兩個函數(shù),主要是元素button的disabled屬性。
Demo:
復(fù)制代碼 代碼如下:
<body>
<a class="disabled">Button disabeld</a> <a class="abled">Button abled</a><br/><br/>
<input type="button" id="submit" name="submit" value="submit">
</body>
<script type="text/javascript" src="../jquery-1.2.6.js"></script>
<script type="text/javascript">
$(function(){
//使其失效
$(".disabled").click(function(){
$("#submit").attr("disabled","disabled");
$("#submit").val('disabled...');
});
//激活
$(".abled").click(function(){
$("#submit").removeAttr("disabled");
$("#submit").val('submit');
});
//操作請求
$("#submit").click(function() {
$("#submit").attr("disabled", "disabled");
alert("hi");//send ajax request
$("#submit").removeAttr("disabled");
});
});
</script>
說明:當點擊"Button disabeld"后,使用 $( "#submit" ).attr("disabled","disabled") 語句禁用測試按鈕,實質(zhì)是給測試按鈕增加一個disabled屬性,然后點擊"Button abled"或執(zhí)行發(fā)送ajax()請求,當請求完全后,使用 $("#submit").removeAttr("disabled"); 語句去除禁用按鈕屬性,從而實現(xiàn)了禁用按鈕,避免重復(fù)發(fā)送請求。
相關(guān)文章
AJAX+Servlet實現(xiàn)的數(shù)據(jù)處理顯示功能示例
這篇文章主要介紹了AJAX+Servlet實現(xiàn)的數(shù)據(jù)處理顯示功能,結(jié)合實例形式分析了前臺ajax與后臺Servlet生成隨機數(shù)顯示的相關(guān)交互操作技巧,需要的朋友可以參考下2018-06-06ajax請求后臺得到j(luò)son數(shù)據(jù)后動態(tài)生成樹形下拉框的方法
今天小編就為大家分享一篇ajax請求后臺得到j(luò)son數(shù)據(jù)后動態(tài)生成樹形下拉框的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08ajax 入門基礎(chǔ)之 XMLHttpRequest對象總結(jié)
在ajax中有一個最為核心的概念就是 XMLHttpRequest 對象,這篇文章將有助于我們更加深入的理解 ajax 的知識2009-08-08