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