如何从javascript中的onclick事件返回值 [英] How to return value from onclick event in javascript

查看:98
本文介绍了如何从javascript中的onclick事件返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<input type='button' id="btnGroups1"   önclick="return fnTabChanges('0')"/>
<input type='button' id="btnGroups2"   önclick="return fnTabChanges('1')"/>
<input type='button' id="btnGroups3"   önclick="return fnTabChanges('2')"/>
<input type='button' id="btnGroups4"   önclick="return fnTabChanges('3')"/>

how to write condition in javascript if third button is click. 
if(fnTabChanges()==2)
{
//code
}



是真的.. 。

请帮助.. ???


is it true...
please help..???

推荐答案

尝试以下内容并根据需要进行调整:



Try the following and adapt it if it suits your need:

<html>
<body>
<input type='button' id="button1" value='first button' onclick="clickMe(this)"/>
<input type='button' id="button2" value='second button' onclick="clickMe(this)"/>
<input type='button' id="button3" value='third button' onclick="clickMe(this)"/>
<input type='button' id="button4" value='fourth button' onclick="clickMe(this)"/>
 
<script>
function clickMe(e){
 
    alert("You have clicked button id = "+e.id+" and button value = "+document.getElementById(e.id).value);
 
}
</script>
 
</body>
</html>


hi
试试这段代码..





hi try this code..


<script type="text/javascript">

       function fnTabChanges(index) {
           if (index == '2')
           { alert(' third button clicked'); }
       }

   </script>


您可以按如下方式返回值



You can return the value as follows

function fnTabChanges(index) {
           //Some condition and code here as per logic
return someValue;
       }





现在检索价值



Now to retrieve the value

önclick="var result = fnTabChanges('0');alert(result);"





//变量result有返回值,alert(结果);被添加到测试目的



希望这有帮助......



// variable result has the return value , alert(result); is added to testing purpose

Hope this helps...


这篇关于如何从javascript中的onclick事件返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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