gpt4 book ai didi

javascript - JavaScript 的重复部分 - 如何整合?

转载 作者:行者123 更新时间:2023-11-28 19:33:47 26 4
gpt4 key购买 nike

我有几段 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' }
}
}

最佳答案

您可以一次绑定(bind)到所有四个 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();
});
});

关于javascript - JavaScript 的重复部分 - 如何整合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26277092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com