简体   繁体   English

无法在javascript onclick事件上调用两个函数

[英]unable to call two functions on javascript onclick event

I want to call two javascript functions, for this i am using 我想调用两个javascript函数,为此我正在使用

<input type="submit" values="submit" onclick="return mandatoryCheck(); submitScore();">

But submitScore() function didn't execute. 但是submitScore()函数没有执行。 Need help... 需要帮忙...

Assuming your are performing some validation check in mandatoryCheck() and its returning true/false . 假设您正在执行mandatoryCheck()校验mandatoryCheck()一些验证检查,并且返回true/false You should use && operator 您应该使用&&运算符

Use 采用

<input type="submit" values="submit" onclick="return mandatoryCheck() && submitScore();">

由于返回,第二个函数不可访问。

<input type="submit" values="submit" onclick="mandatoryCheck(); submitScore();">

Add semi-colons ; 添加分号; to the end of the function calls in order for them both to work. 到函数调用的末尾,以使它们都能正常工作。

<input type="submit" values="submit" onclick="mandatoryCheck(); submitScore();">

Here is a good reference from SitePoint http://reference.sitepoint.com/html/event-attributes/onclick 这是SitePoint的一个很好的参考http://reference.sitepoint.com/html/event-attributes/onclick

Using return inside of a function (in this case, the onclick is a function) will essential "stop" anything past the return statement to happen. 在函数内部使用return (在这种情况下,onclick是一个函数)将本质上“停止”返回语句之后发生的任何事情。

For instance: 例如:

var example = function(){
  return true;
  console.log("Hello.")
}

That will never log because the function returned a value before the other console.log() function could be reached... 那将永远不会记录,因为该函数在到达另一个console.log()函数之前返回了一个值。

That being said, you should redesign your function like this: 话虽如此,您应该像这样重新设计您的功能:

onclick="mandatoryCheck(); submitScore();"

I had a similar problem, selectJQgridRow() was not working. 我有一个类似的问题, selectJQgridRow()无法正常工作。 My input was like: 我的输入就像:

<input type="button" values="Check" onclick="mandatoryCheck(); selectJQgridRow();">

The JavaScript functions were in a separated JavaScript file. JavaScript函数位于单独的JavaScript文件中。 The problem was in the functions definition time. 问题出在函数定义时间。 I had to create selectJQgridRow() after the jQgrid table definition. 我必须在jQgrid表定义之后创建selectJQgridRow() I just put it the last and done. 我只是把它放在最后而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM