我在JavaScript中有几段代码需要简化。变量和大多数逻辑在4个部分的每个部分都是相同的,我该如何简化它?
$(document).ready(function () {
$("#search").click(function (e) {
var ifecha = $("#to").val();
var efecha = $("#from").val();
var prov = $("#CustomerID").val();
var stat = $("#status").val();
if (stat != "Select an Option") {
stat = stat;
}
else {
stat = 3;
}
var anul = $("#anulada").val();
if (anul != "Select an Option") {
anul = anul;
}
else {
anul = "";
}
$.ajax({
type: "GET",
data: { to: ifecha, from: efecha, code: prov, anulada: anul, status: stat, take: 0 },
url: '@Url.Action("proposalReport")',
dataType: "html",
success: function (result) { success(result); }
});
function success(result) {
$("#dataReport").html(result);
}
e.preventDefault();
});
$("#pdf").click(function (e) {
var ifecha = $("#to").val();
var efecha = $("#from").val();
var prov = $("#CustomerID").val();
var stat = $("#status").val();
if (stat != "Select an Option") {
stat = stat;
}
else {
stat = 3;
}
var anul = $("#anulada").val();
if (anul != "Select an Option") {
anul = anul;
}
else {
anul = "";
}
var rep = "PDF"
e.preventDefault();
window.location = 'PrintPDF?report=' + rep + '&to=' + ifecha + '&from=' + efecha + '&code=' + prov + '&anulada=' + anul + '&status=' + stat;
});
$('#lnkView').click(function (e) {
var ifecha = $("#to").val();
var efecha = $("#from").val();
var prov = $("#CustomerID").val();
var stat = $("#status").val();
if (stat != "Select an Option") {
stat = stat;
}
else {
stat = 3;
}
var anul = $("#anulada").val();
if (anul != "Select an Option") {
anul = anul;
}
else {
anul = "";
}
e.preventDefault();
window.open('ReportPrint?to=' + ifecha + '&from=' + efecha + '&code=' + prov + '&anulada=' + anul + '&status=' + stat);
});
$("#excel").click(function (e) {
var ifecha = $("#to").val();
var efecha = $("#from").val();
var prov = $("#CustomerID").val();
var stat = $("#status").val();
if (stat != "Select an Option") {
stat = stat;
}
else {
stat = 3;
}
var anul = $("#anulada").val();
if (anul != "Select an Option") {
anul = anul;
}
else {
anul = "";
}
var rep = "PDF"
e.preventDefault();
window.location = 'ExportToXLS?report=' + rep + '&to=' + ifecha + '&from=' + efecha + '&code=' + prov + '&anulada=' + anul + '&status=' + stat;
});
});
var sorter_options = {
headers: {
3: { sorter: 'datetime' },
4: { sorter: 'decimal' },
5: { sorter: 'decimal' }
}
}
答案 0 :(得分:3)
您可以同时绑定所有四个ID并组合通用逻辑,然后根据所点击的ID来切换相关位:
$(document).ready(function() {
$("#search, #pdf, #lnkView, #excel").click(function(e) {
var ifecha = $("#to").val();
var efecha = $("#from").val();
var prov = $("#CustomerID").val();
var stat = $("#status").val();
if (stat != "Select an Option") {
stat = stat;
} else {
stat = 3;
}
var anul = $("#anulada").val();
if (anul != "Select an Option") {
anul = anul;
} else {
anul = "";
}
switch ($(this).attr('id')) {
case 'search':
$.ajax({
type: "GET",
data: { to: ifecha, from: efecha, code: prov, anulada: anul, status: stat, take: 0 },
url: '@Url.Action("proposalReport")',
dataType: "html",
success: function(result) { $("#dataReport").html(result); }
});
break;
case 'pdf':
var rep = "PDF"
window.location = 'PrintPDF?report=' + rep + '&to=' + ifecha + '&from=' + efecha + '&code=' + prov + '&anulada=' + anul + '&status=' + stat;
break;
case 'lnkView':
window.open('ReportPrint?to=' + ifecha + '&from=' + efecha + '&code=' + prov + '&anulada=' + anul + '&status=' + stat);
break;
case 'excel':
var rep = "PDF"
window.location = 'ExportToXLS?report=' + rep + '&to=' + ifecha + '&from=' + efecha + '&code=' + prov + '&anulada=' + anul + '&status=' + stat;
break;
}
e.preventDefault();
});
});