今天在实现功能的时候发现了一个问题, 在做一个导入的时候,第几次点击导入按钮,就会发送几次请求到后台的问题
页面标签:
<a href="javascript:void(0);" class="dowload" onclick="contributionStatis_setting.exportExcel();">
js 代码(问题代码):
/**
* 导出
*/
exportExcel:function (ids){
$("#tipMask").show();
var statis_time = $("#search_time a[class=cur]").html();
if(statis_time == "全部"){
statis_time = " ";
}
//搜索关键字
//var keyword =$.trim($("#keyword").val());
// 时间
var search_time = $("#search_time a[class=cur]").attr("value");
var beginOn = $("#start").val();
var endOn = $("#end").val();
if (search_time!=undefined){
if (search_time==""){
beginOn =search_time;
}else{
beginOn = contributionStatis_setting.getdesignateDate(search_time);
}
endOn = contributionStatis_setting.getdesignateDate(0);
}else{
if (checkNull(beginOn)) {
tipDialog("fail","请选择开始时间");
return;
}
if (checkNull(endOn)) {
tipDialog("fail","请选择结束时间");
return;
}
}
$("#statis_contribution_export").show();
$("#statis_contribution_export_btn").click(function () {
$(".BoxWrap").hide();
$("#tipMask").hide();
window.document.exportform.action = WEB_URL + "/views/DataContributeList/statis_Contribute_excel?statis_time="+statis_time
+"&beginOn="+beginOn+"&endOn="+endOn+"&keyword="+keyword;
window.document.exportform.submit();
});
},
后来发现是 <a> 标签绑定了 click 事件, 才一直叠加, 出现第几次点击, 发送几次请求的问题, 解决方案是:
先解开绑定的 click 事件 .unbind(‘click‘) 然后在做绑定。
js 代码(正确代码):
/**
* 导出
*/
exportExcel:function (ids){
$("#tipMask").show();
var statis_time = $("#search_time a[class=cur]").html();
if(statis_time == "全部"){
statis_time = " ";
}
//搜索关键字
//var keyword =$.trim($("#keyword").val());
// 时间
var search_time = $("#search_time a[class=cur]").attr("value");
var beginOn = $("#start").val();
var endOn = $("#end").val();
if (search_time!=undefined){
if (search_time==""){
beginOn =search_time;
}else{
beginOn = contributionStatis_setting.getdesignateDate(search_time);
}
endOn = contributionStatis_setting.getdesignateDate(0);
}else{
if (checkNull(beginOn)) {
tipDialog("fail","请选择开始时间");
return;
}
if (checkNull(endOn)) {
tipDialog("fail","请选择结束时间");
return;
}
}
$("#statis_contribution_export").show();
$("#statis_contribution_export_btn").unbind(‘click‘).click(function () {
$(".BoxWrap").hide();
$("#tipMask").hide();
window.document.exportform.action = WEB_URL +
"/views/DataContributeList/statis_Contribute_excel?statis_time="+statis_time
+"&beginOn="+beginOn+"&endOn="+endOn+"&keyword="+keyword;
window.document.exportform.submit();
});
},