在Jquery中按钮未触发的Onclick事件 [英] Onclick Event for button not firing in Jquery

查看:158
本文介绍了在Jquery中按钮未触发的Onclick事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我在表单上创建了一个按钮。当我点击按钮时,会创建一个div面板。这个按钮也在执行它的onclick事件。



我的问题是,onclick事件没有被ShowPanel函数触发/触发()



以下代码如下:



ASP.NET页面:



 <   script     src   =  http://code.jquery.com/jquery-2.1.1.js >  <   / script  >  
< script 类型 = text / javascript >
函数ShowPanel(){
$('#btnSubmit')。trigger('click'); - 调用btnSubmit_Click事件
$('#divcontrol')。toggle(slow);

返回false;
}
< / script >





----创建小div面板并提交按钮---

 <   div     id   =  divcontrol    style   =  background-color:绿色;边框:2纯红色;宽度:300px;高度:100px;颜色:白色;字体大小:30px;显示:无 >  
< / div >

----提交按钮------
& lt; asp:按钮 id = btnSubmit runat = server 文字 = 提交 OnClientClick = javascript:return ShowPanel();
返回false;
OnClick = btnSubmit_Click / >





-------------提交按钮的C#Onclick事件---- ---

 受保护  void  btnSubmit_Click ( object  sender,EventArgs e)
{
GetDetails();
}





单击按钮时会创建div面板,但是onclick事件不会被函数触发/触发(ShowPanel())。



有没有办法从函数中激活btnSubmit_Click事件(ShowPanel()?

解决方案

('#btnSubmit ').trigger('click'); - 调用btnSubmit_Click事件

( '#divcontrol')切换( 慢)。

返回false;
}
< / script >





----创建小div面板并提交按钮---

 <   div     id   =  divcontrol    style   =  background-color:绿色;边框:2纯红色;宽度:300px;高度:100px;颜色:白色;字体大小:30px;显示:无 >  
< / div >

----提交按钮------
& lt; asp:按钮 id = btnSubmit runat = server 文字 = 提交 OnClientClick = javascript:return ShowPanel();
返回false;
OnClick = btnSubmit_Click / >





-------------提交按钮的C#Onclick事件---- ---

 受保护  void  btnSubmit_Click ( object  sender,EventArgs e)
{
GetDetails();
}





单击按钮时会创建div面板,但是onclick事件不会被函数触发/触发(ShowPanel())。



有没有办法从函数中激活btnSubmit_Click事件(ShowPanel()?


OnClientClick调用函数ShowPanel();然后你写的是回归假。哟你也从函数中返回false。因此,它无法触发服务器端方法。



代码如下所示......

 <   asp:按钮    id   =  btnSubmit    runat   =  server   文字  = 提交    OnClientClick   =  javascript:return ShowPanel();      OnClick   =  btnSubmit_Click  /  >  



 <   script     type   =  text / javascript >  
function ShowPanel(){

Hi
I have a button created on a form. When I click on the button a "div" panel is created. This button is also executing its onclick event.

My problem is, the onclick event is not getting triggered/fired from the function ShowPanel()

Here is the code as below:

ASP.NET Page:

<script src="http://code.jquery.com/jquery-2.1.1.js"></script>
    <script type="text/javascript">
        function ShowPanel() {
                $('#btnSubmit').trigger('click'); -- Calling the btnSubmit_Click event
                $('#divcontrol').toggle("slow");

                return false;
            }
    </script>



----Creating the small div panel and submit button---

<div id="divcontrol" style="background-color: Green; border: 2 solid red;width:300px;height:100px;color:White;font-size:30px; display :none">
    </div>

----Submit button------
   <asp:Button id="btnSubmit" runat="server" Text="Submit" OnClientClick="javascript:return ShowPanel();
return false;" OnClick="btnSubmit_Click"/>



-------------C# Onclick event for Submit Button-------

protected void btnSubmit_Click(object sender, EventArgs e)
       {
             GetDetails();
       }



The "div" panel is created when the button is clicked, but the onclick event is not getting fired/triggered from the function (ShowPanel()).

Is there a way how I can fire the btnSubmit_Click event from the function (ShowPanel()?

解决方案

('#btnSubmit').trigger('click'); -- Calling the btnSubmit_Click event


('#divcontrol').toggle("slow"); return false; } </script>



----Creating the small div panel and submit button---

<div id="divcontrol" style="background-color: Green; border: 2 solid red;width:300px;height:100px;color:White;font-size:30px; display :none">
    </div>

----Submit button------
   <asp:Button id="btnSubmit" runat="server" Text="Submit" OnClientClick="javascript:return ShowPanel();
return false;" OnClick="btnSubmit_Click"/>



-------------C# Onclick event for Submit Button-------

protected void btnSubmit_Click(object sender, EventArgs e)
       {
             GetDetails();
       }



The "div" panel is created when the button is clicked, but the onclick event is not getting fired/triggered from the function (ShowPanel()).

Is there a way how I can fire the btnSubmit_Click event from the function (ShowPanel()?


OnClientClick calls the function ShowPanel(); and then you have written "return false;". You are also returning false from the function. So, it can't fire the server side method.

The code should look like below...

<asp:Button id="btnSubmit" runat="server" Text="Submit" OnClientClick="javascript:return ShowPanel(); "  OnClick="btnSubmit_Click"/>


<script type="text/javascript">
        function ShowPanel() {


这篇关于在Jquery中按钮未触发的Onclick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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