繁体   English   中英

使用 JQuery 调用多个按钮单击,这甚至可能吗?

[英]Call multiple button clicks using JQuery, is that even possible?

这是我在 StackOverFlow 上的第一个问题......希望这不是愚蠢的......提前感谢您的帮助!

我想使用 JQuery 示例单击一个按钮来单击多个按钮:Button-1 调用 -> Button-2 AND Button-3

用户应该单击 Button-1,它应该单击 Button-2 和 Button-3。 它还应该触发其相关的服务器端点击事件。

以下是我尝试过的,但它只触发 btnOne Server 事件 click 而不是其他两个。

默认.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"     Inherits="PostBackApp.apppages.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="https://code.jquery.com/jquery-3.6.0.slim.min.js"            integrity="sha256-u7e5khyithlIdTpu22PHhENmPcRdFiHRjhAuHcs05RI="
        crossorigin="anonymous"></script>
    <title></title>
    <script type="text/javascript">
        $(function () {
            $('#btnOne').click(function myfunction() {
                one();
                $('#btnTwo').click();
                $('#btnThree').click();
            });
            $('#btnTwo').click(function myfunction() {
                two();
            });
            $('#btnThree').click(function myfunction() {
                three();
            });
        });
        function one() {
            alert('Client Side One was called');
        }
        function two() {
            alert('Client Side Two was called');
        }
        function three() {
            alert('Client Side Three was called');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="btnOne" Text="1" runat="server" OnClick="btnOne_Click">        </asp:Button>
        <asp:Button ID="btnTwo" Text="2" runat="server" OnClick="btnTwo_Click"></asp:Button>
            <input type="button" name="btnThree" id="btnThree" value="3" />

            </div>
        </form>
    </body>
</html>

默认.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace PostBackApp.apppages

{
    
    public partial class Default : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            bool isPostbck = this.IsPostBack;

        }

        protected void btnOne_Click(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "Key1", "alert('Server Side One was called');", true);
        }

        protected void btnTwo_Click(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "Key2", "alert('Server Side Two was called');", true);
        }
    }
}

您可以在<asp:Button> onClick="yourFunction替换为OnClientClick="yourFunction"以访问您的 JS 函数。

如果同时保留 OnClick 和 OnClientClick,则可以同时触发服务器端 c# 函数和客户端 JS/JQuery 函数。

这个参考帮助我回答了这个问题。 我希望这就是你要找的! :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM