按钮点击通话相同功能 [英] button click call same function

查看:69
本文介绍了按钮点击通话相同功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单击按钮时调用js函数.该函数被调用.我得到按钮对象,并再次调用click()(在同一函数中).我不是很清楚页面生命周期如何发展,但是.该函数不再被调用.我认为-流应该是递归的.任何指导都会有所帮助.
aspx标记:

Calling a js function on button click. The function gets called. I get the button object and call click() on it again(in the same function). I am not very clear of how the page life cycle goes but. the function isn''t called again. What I think is - the flow should have gone recursive. Any guidance would help.
aspx markup:

<asp:Button ID="btnTest" runat="server" Text="Click Me" OnClientClick="testJSfunc();"/> 


javascript


javascript

function testJSfunc() 
{ 
alert('TEST'); 
document.getElementById('<%=btnTest.ClientID %>').click(); 
}

推荐答案

尝试以下操作:

ASPX标记
确保onclick="btnTest_Click"存在
Try the following:

ASPX MARKUP
Make sure onclick="btnTest_Click" exists
<asp:Button ID="btnTest" runat="server" Text="Click Me" OnClientClick="testJSfunc();" onclick="btnTest_Click"/> 



JavaScript



JavaScript

<script type="text/javascript">
    function testJSfunc() {
        alert('TEST');
        __doPostBack('btnTest', 'OnClick');
    }
</script>





CODE BEHIND

protected void btnTest_Click(object sender, EventArgs e)
   {
       /* your code */
   }



如果您有任何麻烦,请告诉我.

[更新]

如果您只想执行两次该功能,则应用以下代码:



Let me know if you have any trouble.

[UPDATED]

If you want to execute the function twice only then apply the following code:

<script type="text/javascript">
       var clickCount = 0;
       function testJSfunc() {
           if (clickCount < 1) {

               alert('TEST');

               //            __doPostBack('btnTest', 'OnClick');

               document.getElementById('<%=btnTest.ClientID %>').click();
           }
           clickCount = clickCount + 1;
       }
   </script>


这篇关于按钮点击通话相同功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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