首页 > 开发 > AJAX > 正文

ajax 防止用户反复提交的2种解决方案

2024-09-01 08:29:02
字体:
来源:转载
供稿:网友
可在服务器端加载方法:
代码如下:
private void OnLoading()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(" function pageLoad(){var manager = Sys.WebForms.PageRequestManager.getInstance(); ");
sb.Append(" manager.add_beginRequest(OnBeginRequest); manager.add_endRequest(OnEndRequest)} var lcPostbackElementID;");
sb.Append(" function OnBeginRequest(sender, args){ lcPostbackElementID = args.get_postBackElement().id.toLowerCase(); if (lcPostbackElementID === /"btnadwords/"){$get(/"btnAdWords/").disabled = true; $get(/"btnAdWords/").value='正在提交...';}} ");
sb.Append(" function OnEndRequest(sender, args){if (lcPostbackElementID === /"btnadwords/"){ $get(/"btnAdWords/").disabled = false; $get(/"btnAdWords/").value='确定';}} ");

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "btnAdWords", sb.ToString(), true);
}

也可直接写在页面脚本里:
代码如下:
<script>
function pageLoad()
{
var manager = Sys.WebForms.PageRequestManager.getInstance();
manager.add_beginRequest(OnBeginRequest);
manager.add_endRequest(OnEndRequest)
}
var lcPostbackElementID;
function OnBeginRequest(sender, args)
{
lcPostbackElementID = args.get_postBackElement().id.toLowerCase();
if (lcPostbackElementID === 'btnadwords')
{
$get('btnAdWords').disabled = true;
$get('btnAdWords').value='正在提交...';
}
}
function OnEndRequest(sender, args)
{
if (lcPostbackElementID === /"btnadwords/")
{
$get('btnAdWords').disabled = false;
$get('btnAdWords').value='确定';
}
}
< /script>

更多内容请参考:http://msdn.microsoft.com/zh-cn/magazine/cc163319.aspx
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表