2010-11-03 47 views
0

我有一个选择框,让用户选择什么样的形式,他需要让我们填写。使用几个JS功能

的2个选项是从一组单人或某人因此一些单个中使用的字段集将与组中的某些字段集进行切换。

所以对于单用户我有这个功能:单信息,获奖信息,记者信息,家长信息。 和组我有群组信息,获奖信息,记者信息。

中的onsubmit我有另一张支票()函数将检查该复选框并选择将调用正确的函数

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

} 

出于某种原因,在完成第一个功能之后,如果回到没有

假它会发送表单而不是停在第二个函数的第一个错误

我该怎么办才能解决它?

+0

你的问题不明确。你想在这里实现什么? – 2010-11-03 10:19:17

回答

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} 
    } 
} 

是正确的方式?

+0

更好的办法是这样做:if(!CheckSingleInformation()||!CheckWinningInformation){return false; } else {return true; }' – amireh 2010-11-03 10:22:41

+0

你用函数'&&'逻辑运算符调用函数。如下所示:'返回CheckFoo()&& CheckBar();'。如果任何一个返回'false',结果将是'false'。如果两者都是“真”,结果将是“真”。 – 2010-11-03 10:26:04

0

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

+0

我知道! :-) – 2010-11-03 10:20:42

0

2说明:

  • 返回 “真” 距离的onsubmit()事件将从传播停止;表单将不会被提交(通常是您在进行AJAX发布时想要的行为)
  • checkWinningInformation();将永远不会在两个块中调用,因为之前的语句是实际跳出的“返回”该方法的执行......这不可能是你想要的,可以吗?
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; 
    } 

} 
0

返回是以函数用于两个原因:

  • 你想脚本停止,如果有事执行。
  • 您希望该函数将值返回给调用函数。

您使用return像这样:

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

你的脚本将 “退出”(或提交表单)第一回后吧!

,你可以做些什么来解决此问题:在结束,以及在CheckGroupInformation()末呼叫CheckWinningInformation()

0

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

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()); 
    } 

}