js书写格式 潜龙勿用

上周项目经理让我写了一段关于用户退款管理的js,花了一段时间自己认为感觉还不错,谁知项目经理看过之后,甚为不满,说我的逻辑有问题,又花了一段时间可惜功亏一篑,不知如何是好,遂求助与项目经理,几分钟之后我的js代码被重新修正,我看了一下,真不愧为大师级人物;下面写下代码块对比一下:

View Code
$(document).ready(function () {
showLogin();
checkBank()
$(
"#cbChoseBank").change(function () {
if ($("#cbChoseBank").attr("checked") == true) {
$(
"#trBankInfo").show();
}
else {
$(
"#trBankInfo").hide();
}
});
var sum = 0;
var id = "";
$(
"input[name='checkbox']").each(function () {
$(
"input[name='checkbox']").removeAttr("checked");
$(
"#hdChoseGoods").val() == "";
$(
this).click(function () {
if ($(this).attr("checked")) {
id
= $(this).val();
if (!id) {
sum
= parseInt(id.split(":")[0] * id.split(":")[1]);
}
else {
sum
+= parseInt(id.split(":")[0] * id.split(":")[1]);
}
}
else {
id
= $(this).val();
sum
-= parseInt(id.split(":")[0] * id.split(":")[1]);
}
$(
"#spanRefuMoney").html(sum);

});
});

});

function checkBank() {
var status = 1;
//退款原因
$("#selRefType").blur(function () {
if ($("#selRefType").val() == "") {
$(
"#spanRefType").html("请选择退款原因");
status
= 2;
return false;
}
else {
$(
"#spanRefType").html("*");
}
});

//开户银行
$("#selBank").blur(function () {
if ($("#selBank").val() == "") {
$(
"#spanBank").html("请选择开户行");
status
= 2;
return false;
}
else {
$(
"#spanBank").html("*");
}
});
//验证银行名称
$("#textBankName").blur(function () {
if ($.trim($("#textBankName").val()) == "") {
$(
"#spanBankName").html("请输入正确的银行名称");
status
= 2;
return false;
}
else {
$(
"#spanBankName").html("*");
}
});
//验证银行账户
$("#textBankCode").blur(function () {
if ($.trim($("#textBankCode").val()) == "" || !CheckIsDigit($("#textBankCode").val())) {
$(
"#spanBankCode").html("请输入正确的银行帐户");
status
= 2;
return false;
}
else {
$(
"#spanBankCode").html("*");
}
});

//验证开户人姓名
$("#textBankUser").blur(function () {
if ($.trim($("#textBankUser").val()) == "") {
$(
"#spanBankUser").html("请输入正确的银行开户人姓名");
status
= 2;
return false;
}
else {
$(
"#spanBankUser").html("*");
}
});

//说明
$("#txtTextarea").blur(function () {
var textarea = $("#txtTextarea").val();
if ($.trim(textarea) == "") {
$(
"#spanTextarea").html("*")
}
else if (textarea.length > 100) {
$(
"#spanTextarea").html("最多可输入100个字符 ");
status
= 2;
return false;
}
});

//验证密码是否正确
$("#pwd").blur(function () {
var strPassword = $("#pwd").val();
if ($.trim(strPassword).length == 0) {
$(
"#message").css("color", "red");
$(
"#message").html(" 密码输入不能为空!");
status
= 2;
return false;
}
$.ajax({
type:
"POST",
url:
"/Control/Query.ashx",
data:
"URL", //加随机参数去缓存
beforeSend: function () {
$(
"#message").css("color", "#999");
},
success:
function (msg) {
if (msg == "1") {
$(
"#message").css("color", "green");
$(
"#message").html(" 恭喜您,密码已通过验证");
$(
"#PwdPass").val("1"); //表示密码通过验证

}
else {
status
= 2;
$(
"#message").css("color", "red");
$(
"#message").html(" 密码验证不正确,请重新输入");
return false;
}
},
error:
function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
}
});
});

$(
"#btnRefund").click(function () {
var id_z = 0;
var gds_id = "";
$(
"input[name='checkbox']").each(function () {
if ($(this).attr("checked")) {
gds_id
+= ($(this).val()).split(":")[2] + "#" + ($(this).val()).split(":")[0] + "|";
id_z
++;
}
});
$(
"#hdChoseGoods").val(gds_id);
status
= 1;
$(
"#selRefType").blur();
$(
"#selBank").blur();
$(
"#textBankName").blur();
$(
"#textBankCode").blur();
$(
"#textBankUser").blur();
$(
"#txtTextarea").blur();
$(
"#pwd").blur();
$(
"#hdChoseGoods").val(gds_id);
if (status == 2) {
$(
"#btnRefund").attr("disable", "");
}
else if (id_z <= 0) {
alert(
"您当前没选中任何项!");
$(
"#btnRefund").attr("disable", "");
}
else {
$(
"#btnRefund").removeAttr("disable", "");
var ord = $("#ordNumber").val();
$.ajax({
type:
"Post",
url:
"url",
data: { "data":data
},
success:
function (msg) {
if (msg > 0) {
alert(
"添加成功!");
location.href
= "/ordercenter/refundlist.html";
}
else {
alert(
"失败");
}
}

});

}
});

}

//验证是否为数字
function CheckIsDigit(value) {
var digit = /^\+?[0-9][0-9]*$/;
if (!digit.exec(value)) {
return false;
}
else {
return true;
}
}
View Code
$(document).ready(function () {
showLogin();
CheckContent();
var sum = 0;
var id = "";
$(
"input[name='checkbox']").each(function () {
$(
"input[name='checkbox']").removeAttr("checked");
$(
"#hdChoseGoods").val() == "";
$(
this).click(function () {
if ($(this).attr("checked")) {
id
= $(this).val();
if (!id) {
sum
= parseInt(id.split(":")[0] * id.split(":")[1]);
}
else {
sum
+= parseInt(id.split(":")[0] * id.split(":")[1]);
}
}
else {
id
= $(this).val();
sum
-= parseInt(id.split(":")[0] * id.split(":")[1]);
}
$(
"#spanRefuMoney").html(sum);

});
});
$(
"#btnRefund").click(checkBank);

});
function CheckContent() {
$(
"#selRefType").blur(function () {
if ($("#selRefType").val() == "") {
$(
"#spanRefType").html("请选择退款原因");
}
else {
$(
"#spanRefType").html("*");
}
});

//开户银行
$("#selBank").blur(function () {
if ($("#selBank").val() == "") {
$(
"#spanBank").html("请选择开户行");
}
else {
$(
"#spanBank").html("*");
}
});
//验证银行名称
$("#textBankName").blur(function () {
if ($.trim($("#textBankName").val()) == "") {
$(
"#spanBankName").html("请输入正确的银行名称");
}
else {
$(
"#spanBankName").html("*");
}
});
//验证银行账户
$("#textBankCode").blur(function () {
if ($.trim($("#textBankCode").val()) == "" || !CheckIsDigit($("#textBankCode").val())) {
$(
"#spanBankCode").html("请输入正确的银行帐户");
}
else {
$(
"#spanBankCode").html("*");
}
});

//验证开户人姓名
$("#textBankUser").blur(function () {
if ($.trim($("#textBankUser").val()) == "") {
$(
"#spanBankUser").html("请输入正确的银行开户人姓名");
}
else {
$(
"#spanBankUser").html("*");
}
});

//说明
$("#txtTextarea").blur(function () {
var textarea = $("#txtTextarea").val();
if (textarea.length > 100) {
$(
"#spanTextarea").html("最多可输入100个字符 ");
}
else {
$(
"#spanTextarea").html("");
}
});
//验证密码是否正确
$("#pwd").blur(function () {
if ($.trim($("#pwd").val()).length == 0) {
$(
"#message").css("color", "red");
$(
"#message").html(" 密码输入不能为空!");
}
else {
$(
"#message").html("*");
}
});
}




function checkBank() {
var id_z = 0;
var gds_id = "";
$(
"input[name='checkbox']").each(function () {
if ($(this).attr("checked")) {
gds_id
+= ($(this).val()).split(":")[2] + "#" + ($(this).val()).split(":")[0] + "|";
id_z
++;
}
});
if (id_z <= 0) {
alert(
"您当前没选中任何项!");
return;
}
$(
"#hdChoseGoods").val(gds_id);

$(
"#selRefType").blur();
if ($("#spanRefType").html().length > 1) return;

$(
"#selBank").blur();
if ($("#spanBank").html().length > 1) return;

$(
"#textBankName").blur();
if ($("#spanBankName").html().length > 1) return;

$(
"#textBankCode").blur();
if ($("#spanBankCode").html().length > 1) return;

$(
"#textBankUser").blur();
if ($("#spanBankUser").html().length > 1) return;

$(
"#txtTextarea").blur();
if ($("#spanTextarea").html().length > 0) return;

$(
"#pwd").blur();
if ($("#message").html().length > 1) return;

CheckPwd();
}

//验证是否为数字
function CheckIsDigit(value) {
var digit = /^\+?[0-9][0-9]*$/;
if (!digit.exec(value)) {
return false;
}
else {
return true;
}
}
function CheckPwd() {
$.ajax({
type:
"POST",
url:
"url",
data:
"data", //加随机参数去缓存
beforeSend: function () {
$(
"#message").css("color", "#999");
},
success:
function (msg) {
if (msg == "1") {
$(
"#message").css("color", "green");
$(
"#message").html("*");
ExecSubmit();
//提交退款
}
else {
$(
"#message").css("color", "red");
$(
"#message").html(" 密码验证不正确,请重新输入");
}
},
error:
function (XMLHttpRequest, textStatus, errorThrown) {
$(
"#message").html(" 密码验证不正确,请重新输入");
}
});
}

function ExecSubmit() {
$(
"#btnRefund").removeAttr("disable", "");
$.ajax({
type:
"Post",
url:
"url",
data: { "data":data
},
success:
function (msg) {
if (msg == 3) {
alert(
"添加成功!");
location.href
= "/ordercenter/refundlist.html";
}
else if (msg == 1) {
alert(
"订单号已存在");
}
else if (msg == 9) {
alert(
"该用户下不存在此订单");
}
else if (msg == 2) {
alert(
"已过退款日期");
}
else {
alert(
"失败");
}
}
});
}

呵呵,比较了一下,之间的差别不是一般的大啊,作为菜鸟赛季的我,看出了与大师之间的差别,不服还真不行啊,希望大家以后养成良好的编码习惯。

原文地址:https://www.cnblogs.com/dragonbattlesun/p/2063041.html