如何从代码背后触发jQuery函数

本文关键字:jQuery 函数 背后 代码 | 更新日期: 2023-09-27 18:24:07

我正试图从ASP.NET代码背后调用一个jQuery函数。

我不希望它被jQuery点击事件触发;我想触发控件后面代码中的jQuery函数--->"显示对话框"。

$(document).ready(function () {
    $("#LinkButton1").click(function(){
        $("#hightlight_show1").toggle();
    });
    var dialog1 = $("#Add").dialog({
            autoOpen: false,
            width: 620,
            height: 400
    });
    // Move the dialog back into the <form> element 
    dialog1.parent().appendTo(jQuery("form:first"));
    $("#BT_add").click(function () {
       $("#Add").dialog("open");
       return false;
});

我试过这样的东西,但没有奏效:

$(document).ready(function () {
    $("#LinkButton1").click(function () {
        $("#hightlight_show1").toggle();
    });
    var dialog1 = $("#Add").dialog({
        autoOpen: false,
        width: 620,
        height: 400
    });
    // Move the dialog back into the <form> element 
    dialog1.parent().appendTo(jQuery("form:first"));
    function a() {
        $("#Add").dialog("open");
        return false;
    };
});

我使用Page.ClientScript.RegisterStartupScript():在后面的代码中指定了这一点

Page.ClientScript.RegisterStartupScript(
    this.GetType(),
    "a",
    "a();",
    true
 );

如何从代码背后触发jQuery函数

首先,将function a() {移出$(document).ready()

但你可能仍然对处决顺序有意见。

如果这是完全回发,那么用另一个$(document).ready()包装RegisterStartupScript中的a()调用。如果是来自UpdatePanel的部分回发,那么只要在第一次加载页面时正确创建了对话框,它就应该可以工作。

无法从代码背后触发JavaScript函数。您可以在html页面上定义JavaScript函数,并使用Control.Attributes.Add在服务器端绑定点击事件。您需要服务器控制来绑定事件,您可以通过放置runat="server"使html控制服务器可访问

Javascript

function  YourFun(){
    $("#hightlight_show1").toggle();
}

代码隐藏

hightlight_show1.Attributes.Add("onclick", "YourFun();");

服务器端(c#)操作似乎很难影响客户端(jQuery)事件。您需要使用ajax从启动事件的服务器请求信息。

记住,c#/ASP代码运行在web服务器上,它的工作只是根据请求向浏览器发送HTML。之后,它的作用基本上就完成了。JavaScript和jQuery等由浏览器进行解释,但仅在文档离开服务器很久之后。有一些"方法"可以让c#在非常松散的意义上触发jQuery事件,但最终你仍然必须认识到客户端(浏览器)和web服务器之间的区别。

您在document.ready函数中声明的function a()

Register.StartupScript将把代码添加到document.onload,你确定哪个事件正在启动(就绪或加载)吗?。

在document.ready之外声明它,它可能会起作用。类似这样的东西:

<script type="text/javascript>
function a() {
    $("#Add").dialog("open");
    return false;
};
$(document).ready(function () {
     $("#LinkButton1").click(function(){
         $("#hightlight_show1").toggle();
     });
     // Move the dialog back into the <form> element 
     dialog1.parent().appendTo(jQuery("form:first"));
     var dialog1 = $("#Add").dialog({
         autoOpen: false,
         width: 620,
         height: 400
     });
});

执行此

加价

<script type="text/javascript">
    function ShowPopup(message) {
        $(function () {
            $("#dialog").html(message);
            $("#dialog").dialog({
                title: "jQuery Dialog Popup",
                buttons: {
                    Close: function () {
                        $(this).dialog('close');
                    }
                },
                modal: true
            });
        });
    };
</script>
<div id="dialog" style="display: none">
</div>
<asp:Button ID="btnShowPopup" runat="server" Text="Show Popup" OnClick="btnShowPopup_Click" />

代码隐藏

  protected void btnShowPopup_Click(object sender, EventArgs e)
{
    string message = "Message from server side";
    ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('" + message + "');", true);
}

在C#和VB 中下载工作演示

不确定这是否真的是你的问题,但在"你尝试的代码"中,你的脚本标记声明中缺少了右双引号(")。

您写道:type="text/javascript

但从未关闭声明。。。这肯定会把JS搞得一团糟。