Livecycle javascript ...压缩 if 语句
Livecycle javascript...condense if statement
我正在制作一个包含 "Validate" 按钮的表单。此按钮的目的是检查并确保所有字段都已完成(这是项目要求的)。下面是检查字段是否为 null
然后更改边框颜色并显示文本框的代码。
if (form1.Main.sfRequestor.requestNameFirst.rawValue == null){
form1.Main.sfRequest.txtValidate.presence = "visible";
form1.Main.sfRequestor.requestNameFirst.border.edge.color.value = "255,0,0"
} else {
form1.Main.sfRequest.txtValidate.presence = "hidden";
form1.Main.sfRequestor.requestNameFirst.border.edge.color.value = "255,255,255"
};
if (form1.Main.sfRequestor.requestNameLast.rawValue == null){
form1.Main.sfRequest.txtValidate.presence = "visible";
form1.Main.sfRequestor.requestNameLast.border.edge.color.value = "255,0,0"
} else {
form1.Main.sfRequest.txtValidate.presence = "hidden";
form1.Main.sfRequestor.requestNameLast.border.edge.color.value = "255,255,255"
};
多个子表单中有 20 多个字段需要检查。我正在尝试合并代码,但不知道如何操作。变量可以处理 Javascript 中的字段名称吗?
您可以轻松地将它变成一个简单的函数:
function validateField(element) {
if (element.rawValue == null) {
form1.Main.sfRequest.txtValidate.presence = "visible";
element.border.edge.color.calue = "255,0,0";
}
else {
form1.Main.sfRequest.txtValidate.presence = "hidden";
element.border.edge.color.calue = "255,255,255";
}
}
然后就这样调用它:
validateField(form1.Main.sfRequestor.requestNameFirst);
validateField(form1.Main.sfRequestor.requestNameLast);
为了进一步简化,将所有 20 个元素放在一个数组中并循环
var elements = [form1.Main.sfRequestor.requestNameFirst, form1.Main.sfRequestor.requestNameLast, ...];
elements.forEach(function(element) {
validateField(element);
});
你可以很容易地把它变成一个循环,并且可以把它写成一个 IIFE 来保持你的命名空间干净
(function (arr) {
var txtValidate = form1.Main.sfRequest.txtValidate,
i, e;
for (i = 0; i < arr.length; ++i) {
e = form1.Main.sfRequestor[arr[i]]; // cache me
if (e.rawValue == null){
txtValidate.presence = "visible";
e.border.edge.color.value = "255,0,0"
} else {
txtValidate.presence = "hidden";
e.border.edge.color.value = "255,255,255"
}
}
}(['requestNameFirst', 'requestNameLast']));
但是,看起来 txtValidate.presence
只会设置为最后一项的条件,您确定不想使用标志并将其设置为最后一项吗?例如
(function (arr) {
var txtValidateState = 'hidden',
i, e;
for (i = 0; i < arr.length; ++i) {
e = form1.Main.sfRequestor[arr[i]];
if (e.rawValue == null){
txtValidateState = "visible"; // any null makes txtValidate visible
e.border.edge.color.value = "255,0,0"
} else {
e.border.edge.color.value = "255,255,255"
}
}
form1.Main.sfRequest.txtValidate.presence = txtValidateState; // set last
}(['requestNameFirst', 'requestNameLast']));
更新通用表单,假设 sfRequestor
和 sfRequest
(function (form, arr) {
var txtValidateState = 'hidden',
i, e;
for (i = 0; i < arr.length; ++i) {
e = form.sfRequestor[arr[i]];
if (e.rawValue == null){
txtValidateState = "visible";
e.border.edge.color.value = "255,0,0";
} else {
e.border.edge.color.value = "255,255,255";
}
}
form.sfRequest.txtValidate.presence = txtValidateState;
}(form1.Main, ['requestNameFirst', 'requestNameLast']));
更新假设 sfRequest
是一个常量,但 sfRequestor
可能不同
(function () { // moved IIFE to protect namespace
function validate(form, subform, arr) { // now named, new param subform
var txtValidateState = 'hidden',
i, e;
for (i = 0; i < arr.length; ++i) {
e = form[subform][arr[i]]; // select from subform
if (e.rawValue == null){
txtValidateState = "visible";
e.border.edge.color.value = "255,0,0";
} else {
e.border.edge.color.value = "255,255,255";
}
}
form.sfRequest.txtValidate.presence = txtValidateState; // assuming stays same
}
validate(form1.Main, 'sfRequestor', ['requestNameFirst', 'requestNameLast']);
validate(form1.Main, 'sfClientInfo', ['firstname']);
// if you have many here you can re-write as a loop again
}());
我正在制作一个包含 "Validate" 按钮的表单。此按钮的目的是检查并确保所有字段都已完成(这是项目要求的)。下面是检查字段是否为 null
然后更改边框颜色并显示文本框的代码。
if (form1.Main.sfRequestor.requestNameFirst.rawValue == null){
form1.Main.sfRequest.txtValidate.presence = "visible";
form1.Main.sfRequestor.requestNameFirst.border.edge.color.value = "255,0,0"
} else {
form1.Main.sfRequest.txtValidate.presence = "hidden";
form1.Main.sfRequestor.requestNameFirst.border.edge.color.value = "255,255,255"
};
if (form1.Main.sfRequestor.requestNameLast.rawValue == null){
form1.Main.sfRequest.txtValidate.presence = "visible";
form1.Main.sfRequestor.requestNameLast.border.edge.color.value = "255,0,0"
} else {
form1.Main.sfRequest.txtValidate.presence = "hidden";
form1.Main.sfRequestor.requestNameLast.border.edge.color.value = "255,255,255"
};
多个子表单中有 20 多个字段需要检查。我正在尝试合并代码,但不知道如何操作。变量可以处理 Javascript 中的字段名称吗?
您可以轻松地将它变成一个简单的函数:
function validateField(element) {
if (element.rawValue == null) {
form1.Main.sfRequest.txtValidate.presence = "visible";
element.border.edge.color.calue = "255,0,0";
}
else {
form1.Main.sfRequest.txtValidate.presence = "hidden";
element.border.edge.color.calue = "255,255,255";
}
}
然后就这样调用它:
validateField(form1.Main.sfRequestor.requestNameFirst);
validateField(form1.Main.sfRequestor.requestNameLast);
为了进一步简化,将所有 20 个元素放在一个数组中并循环
var elements = [form1.Main.sfRequestor.requestNameFirst, form1.Main.sfRequestor.requestNameLast, ...];
elements.forEach(function(element) {
validateField(element);
});
你可以很容易地把它变成一个循环,并且可以把它写成一个 IIFE 来保持你的命名空间干净
(function (arr) {
var txtValidate = form1.Main.sfRequest.txtValidate,
i, e;
for (i = 0; i < arr.length; ++i) {
e = form1.Main.sfRequestor[arr[i]]; // cache me
if (e.rawValue == null){
txtValidate.presence = "visible";
e.border.edge.color.value = "255,0,0"
} else {
txtValidate.presence = "hidden";
e.border.edge.color.value = "255,255,255"
}
}
}(['requestNameFirst', 'requestNameLast']));
但是,看起来 txtValidate.presence
只会设置为最后一项的条件,您确定不想使用标志并将其设置为最后一项吗?例如
(function (arr) {
var txtValidateState = 'hidden',
i, e;
for (i = 0; i < arr.length; ++i) {
e = form1.Main.sfRequestor[arr[i]];
if (e.rawValue == null){
txtValidateState = "visible"; // any null makes txtValidate visible
e.border.edge.color.value = "255,0,0"
} else {
e.border.edge.color.value = "255,255,255"
}
}
form1.Main.sfRequest.txtValidate.presence = txtValidateState; // set last
}(['requestNameFirst', 'requestNameLast']));
更新通用表单,假设 sfRequestor
和 sfRequest
(function (form, arr) {
var txtValidateState = 'hidden',
i, e;
for (i = 0; i < arr.length; ++i) {
e = form.sfRequestor[arr[i]];
if (e.rawValue == null){
txtValidateState = "visible";
e.border.edge.color.value = "255,0,0";
} else {
e.border.edge.color.value = "255,255,255";
}
}
form.sfRequest.txtValidate.presence = txtValidateState;
}(form1.Main, ['requestNameFirst', 'requestNameLast']));
更新假设 sfRequest
是一个常量,但 sfRequestor
可能不同
(function () { // moved IIFE to protect namespace
function validate(form, subform, arr) { // now named, new param subform
var txtValidateState = 'hidden',
i, e;
for (i = 0; i < arr.length; ++i) {
e = form[subform][arr[i]]; // select from subform
if (e.rawValue == null){
txtValidateState = "visible";
e.border.edge.color.value = "255,0,0";
} else {
e.border.edge.color.value = "255,255,255";
}
}
form.sfRequest.txtValidate.presence = txtValidateState; // assuming stays same
}
validate(form1.Main, 'sfRequestor', ['requestNameFirst', 'requestNameLast']);
validate(form1.Main, 'sfClientInfo', ['firstname']);
// if you have many here you can re-write as a loop again
}());