在表单检查上使用几个JS函数

时间:2010-11-03 10:15:32

标签: javascript

我有一个选择框,允许用户选择他需要填写的表格。

这两个选项适用于单个人或某个群组中的某个人,因此单个人中使用的某些字段集会与组中的某些字段集进行切换。

所以对于单个用户我有这个功能:单一信息,获胜信息,报告信息,父母信息。 对于小组我有小组信息,获奖信息,记者信息。

在onsubmit中我有另一个check()函数,它将检查复选框,并通过选择将调用正确的函数

function Check() {
    if (document.getElementById("type").value=="s") {
        return CheckSingleInformation();
        return CheckWinningInformation();
    }
    else if (document.getElementById("type").value=="g") {
        return CheckGroupInformation();
        return CheckWinningInformation();
    }

}

由于某种原因,在完成第一个函数后,如果它返回没有false,它将发送表单而不是停止在第二个函数的第一个错误

我该怎么做才能解决它?

6 个答案:

答案 0 :(得分:0)

onsubmit必须具有值“return Check()”。如果您只是调用Check(),则不会使用返回值,它将继续提交。

答案 1 :(得分:0)

没关系......找到了答案: - )

首先我将主要功能更改为:

function Check() { 
    if (document.getElementById("type").value=="s") { 
        CheckSingleInformation(); 
        CheckWinningInformation(); 
    } 
    else if (document.getElementById("type").value=="g") { 
        CheckGroupInformation(); 
        CheckWinningInformation(); 
    } 

} 

然后我发现它在第一个功能之后不会停止所以我把它改成了这样的

function Check() {
    if (document.getElementById("type").value=="s") {
        if (CheckSingleInformation()==false) {return false}
        if (CheckWinningInformation()==false) {return false}
    }
    else if (document.getElementById("type").value=="g") {
        if (CheckGroupInformation()==false) {return false}
        if (CheckWinningInformation()==false) {return false}
    }
}

这是正确的方法吗?

答案 2 :(得分:0)

2注:

  • 从onsubmit()事件返回“true”将阻止它传播;表单将不会被提交(通常是您在进行AJAX发布时所需的行为)
  • checkWinningInformation();将永远不会在两个块中被调用,因为它之前的语句是一个“返回”,它实际上跳出了方法执行...这可能不是你想要的,是吗?

答案 3 :(得分:0)

检查嵌套条件,

function Check() {
    if (document.getElementById("type").value=="s") {
        if(!CheckSingleInformation()){
           return CheckWinningInformation();
        }
        return true;
    }
    else if (document.getElementById("type").value=="g") {
        if(!CheckGroupInformation()){
            return CheckWinningInformation();
        }
        return true;
    }

}

答案 4 :(得分:0)

返回在函数中使用有两个原因:

  • 如果发生某些事情,您希望脚本停止执行。
  • 您希望函数将值返回给调用函数。

您使用了return,如此:

return CheckSingleInformation(); <-- exit here
return CheckWinningInformation();

您的脚本将在第一次返回后立即“退出”(或提交表单)!

您可以采取以下措施来解决此问题:在CheckWinningInformation()末尾以及CheckSingleInformation()结束时致电CheckGroupInformation()

答案 5 :(得分:0)

使用逻辑AND运算符(&amp;&amp;)可以。 所以下面的代码:

function Check() {
    if (document.getElementById("type").value=="s") {
        return CheckSingleInformation();
        return CheckWinningInformation();
    }
    else if (document.getElementById("type").value=="g") {
        return CheckGroupInformation();
        return CheckWinningInformation();
    }

}

可能是:

function Check() {
    if (document.getElementById("type").value=="s") {         
        return (CheckWinningInformation() && CheckSingleInformation());
    }
    else if (document.getElementById("type").value=="g") {        
        return (CheckWinningInformation() && CheckGroupInformation());
    }

}