在重定向到不同功能的同一功能上使用多个按钮 [英] Using multiple buttons on same function that redirects to different functions

查看:97
本文介绍了在重定向到不同功能的同一功能上使用多个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有多个按钮并单击所有按钮他们正在将我重定向到相同的功能,并从该功能转到为该按钮指定的另一个功能。
任何想法如何才能真正知道点击哪个按钮的功能?
示例:

 < html> 

< button type =buttononclick =myFunction()id =1> Button1< / button>
< button type =buttononclick =myFunction()id =2> Button1< / button>
< button type =buttononclick =myFunction()id =3> Button1< / button>

< script>

函数myFunction()
{
var x = 0;

if(button 1){
x = 1;
myFunction1(x);}

if(button 2){
x = 2;
myFunction2(x);}

if(button 3){
x = 3;
myFunction3(x);}

...
myFunction3(x){
alert(x);
}
}

< / script>

< / html>


解决方案

最简单的方法可能是将元素传入该功能:

 < button type =buttononclick =myFunction(this)id =1>按钮1< /按钮> 
< button type =buttononclick =myFunction(this)id =2> Button1< / button>
< button type =buttononclick =myFunction(this)id =3> Button1< / button>

函数myFunction(elem){
alert(elem.id);
}

不需要考虑事件参数或类似的事情。


So..i'm having this problem for couple of days not knowing how to do this,and i need help.

I have multiple buttons and clicking all of them is redirecting me to same function and from that function is going to another function specified for that button. Any idea how can i go true couple of functions knowing which button is clicked? example :

 <html>

<button type="button" onclick="myFunction()" id="1">Button1</button>
<button type="button" onclick="myFunction()" id="2">Button1</button>
<button type="button" onclick="myFunction()" id="3">Button1</button>

<script>

function myFunction()
{
   var x=0;

    if (button 1){
     x=1;
     myFunction1(x);}

   if (button 2){
    x=2;
    myFunction2(x);}

    if (button 3){
     x=3;
     myFunction3(x);}

     ...
    myFunction3(x){
    alert(x);
}
}

</script>

</html>

解决方案

Easiest way would probably be to pass in the element into the function:

<button type="button" onclick="myFunction(this)" id="1">Button1</button>
<button type="button" onclick="myFunction(this)" id="2">Button1</button>
<button type="button" onclick="myFunction(this)" id="3">Button1</button>

function myFunction(elem) {
    alert(elem.id);
}

No need to think about event arguments or anything like that.

这篇关于在重定向到不同功能的同一功能上使用多个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆