从同一个onclick [duplicate]调用两个函数

时间:2021-10-18 16:54:01

This question already has an answer here:

这个问题已经有了答案:

HTML & JS

HTML和javascript

How do I call 2 functions from one onclick event? Here's my code

如何从一个onclick事件调用两个函数?这是我的代码

 <input id ="btn" type="button" value="click" onclick="pay() cls()"/>

the two functions being pay() and cls(). Thanks!

这两个函数是pay()和cls()。谢谢!

9 个解决方案

#1


142  

Add semi-colons ; to the end of the function calls in order for them both to work.

添加分号;在函数的末尾调用,以便它们都能工作。

 <input id="btn" type="button" value="click" onclick="pay(); cls();"/>

I don't believe the last one is required but hey, might as well add it in for good measure.

我不认为最后一个是必需的,但是嘿,不妨把它加进去。

Here is a good reference from SitePoint http://reference.sitepoint.com/html/event-attributes/onclick

这里有一个来自SitePoint http://reference.sitepoint.com/html/event-attributes/onclick的很好的引用

#2


30  

You can create a single function that calls both of those, and then use it in the event.

您可以创建一个单独的函数来调用它们,然后在事件中使用它。

function myFunction(){
    pay();
    cls();
}

And then, for the button:

然后,对于按钮:

<input id="btn" type="button" value="click" onclick="myFunction();"/>

#3


15  

You can call the functions from inside another function

可以从另一个函数内部调用函数

<input id ="btn" type="button" value="click" onclick="todo()"/>

function todo(){
pay(); cls();
}

#4


7  

With jQuery :

jQuery:

jQuery("#btn").on("click",function(event){
    event.preventDefault();
    pay();
    cls();
});

#5


7  

Just to offer some variety, the comma operator can be used too but some might say "noooooo!", but it works:

为了提供一些多样性,逗号运算符也可以使用,但是有些人可能会说“noooo !”,但它的工作原理:

<input type="button" onclick="one(), two(), three(), four()"/>

http://jsbin.com/oqizir/1/edit

http://jsbin.com/oqizir/1/edit

#6


6  

onclick="pay(); cls();"

however, if you're using a return statement in "pay" function the execution will stop and "cls" won't execute,

但是,如果在“pay”函数中使用返回语句,则执行将停止,而“cls”不会执行,

a workaround to this:

一个解决方案:

onclick="var temp = function1();function2(); return temp;"

#7


6  

Binding events from html is NOT recommended. This is recommended way:

不建议从html绑定事件。这是推荐的方式:

document.getElementById('btn').addEventListener('click', function(){
    pay();
    cls();
});

#8


5  

put a semicolon between the two functions as statement terminator.

将两个函数之间的分号作为语句结束符。

#9


4  

Try this

试试这个

<input id ="btn" type="button" value="click" onclick="pay();cls()"/>

#1


142  

Add semi-colons ; to the end of the function calls in order for them both to work.

添加分号;在函数的末尾调用,以便它们都能工作。

 <input id="btn" type="button" value="click" onclick="pay(); cls();"/>

I don't believe the last one is required but hey, might as well add it in for good measure.

我不认为最后一个是必需的,但是嘿,不妨把它加进去。

Here is a good reference from SitePoint http://reference.sitepoint.com/html/event-attributes/onclick

这里有一个来自SitePoint http://reference.sitepoint.com/html/event-attributes/onclick的很好的引用

#2


30  

You can create a single function that calls both of those, and then use it in the event.

您可以创建一个单独的函数来调用它们,然后在事件中使用它。

function myFunction(){
    pay();
    cls();
}

And then, for the button:

然后,对于按钮:

<input id="btn" type="button" value="click" onclick="myFunction();"/>

#3


15  

You can call the functions from inside another function

可以从另一个函数内部调用函数

<input id ="btn" type="button" value="click" onclick="todo()"/>

function todo(){
pay(); cls();
}

#4


7  

With jQuery :

jQuery:

jQuery("#btn").on("click",function(event){
    event.preventDefault();
    pay();
    cls();
});

#5


7  

Just to offer some variety, the comma operator can be used too but some might say "noooooo!", but it works:

为了提供一些多样性,逗号运算符也可以使用,但是有些人可能会说“noooo !”,但它的工作原理:

<input type="button" onclick="one(), two(), three(), four()"/>

http://jsbin.com/oqizir/1/edit

http://jsbin.com/oqizir/1/edit

#6


6  

onclick="pay(); cls();"

however, if you're using a return statement in "pay" function the execution will stop and "cls" won't execute,

但是,如果在“pay”函数中使用返回语句,则执行将停止,而“cls”不会执行,

a workaround to this:

一个解决方案:

onclick="var temp = function1();function2(); return temp;"

#7


6  

Binding events from html is NOT recommended. This is recommended way:

不建议从html绑定事件。这是推荐的方式:

document.getElementById('btn').addEventListener('click', function(){
    pay();
    cls();
});

#8


5  

put a semicolon between the two functions as statement terminator.

将两个函数之间的分号作为语句结束符。

#9


4  

Try this

试试这个

<input id ="btn" type="button" value="click" onclick="pay();cls()"/>