如何在JavaScript中以及在哪里将这两个变量结合在一起?

时间:2019-02-19 15:15:56

标签: javascript pdf pdf-form action-button

我将此代码拼凑在一起,链接到本PDF中的操作按钮。应该验证是否输入了所有必填字段,如果这样,它将从文档中提取姓名和员工编号并生成带有PDF的电子邮件。如果未通过这些检查,则会弹出一个窗口,警告用户缺少的信息。

我的问题是代码运行时,您可以绕过警告并仍通过电子邮件发送文档。

我如何调整它以使其停止直到所有字段都被回答?

所需的“伪代码”: 检查空白字段(获取管理者的姓名)(获取管理者的EE数字键)。如果必填字段(复选框)和注释为空,请警告用户。 如果很好,请从这些区域构建电子邮件主题行和文本...

这是现在的代码:

var emptyFields = [];

for (var i = 0; i < this.numFields; i++) {
    var f = this.getField(this.getNthFieldName(i));

    if (f.type != "button" && f.required) {
        if ((f.type == "text" && f.value == "") || (f.type == "checkbox" && f.value == "Off")) emptyFields.push(f.name);
    }
}

if (emptyFields.length > 0) {
    app.alert("Error! This checklist is incomplete:\n" + emptyFields.join("\n"));
}

// Build the subject line text from several fields form fields
var subj_text = getField("Weekly Management Critical Items:").valueAsString;
subj_text += ": " + getField("Manager Name").value;
subj_text += "- " + getField("Manager EE#").valueAsString;

// Send the email
mailDoc({
    cTo: "dennis.aikens@fedex.com",

    cSubject: subj_text,

    cMsg: "Hey Dennis, here's my checklist for this week. \r" + "Let me know if you have any questions. Thank you."
});

我知道它有点长,所以感谢您的阅读。

感谢您的所有时间和投入!

1 个答案:

答案 0 :(得分:1)

  

您应该写下干净的控制流程代码...         现在您可以使用

var emptyFields = [];

for (var i = 0; i < this.numFields; i++) {
    var f = this.getField(this.getNthFieldName(i));

    if (f.type != "button" && f.required) {
        if ((f.type == "text" && f.value == "") || (f.type == "checkbox" && f.value == "Off")) emptyFields.push(f.name);
    }
}

if (emptyFields.length > 0) {
    app.alert("Error! This checklist is incomplete:\n" + emptyFields.join("\n"));
}else{
sendMail();
}


var sendMail = function(){
// Build the subject line text from several fields form fields
var subj_text = getField("Weekly Management Critical Items:").valueAsString;
subj_text += ": " + getField("Manager Name").value;
subj_text += "- " + getField("Manager EE#").valueAsString;

// Send the email
mailDoc({
    cTo: "dennis.aikens@fedex.com",

    cSubject: subj_text,

    cMsg: "Hey Dennis, here's my checklist for this week. \r" + "Let me know if you have any questions. Thank you."
});
}