javascript - jquery 回发失败

标签 javascript jquery

我正在尝试启动回发,调用服务器运行元素的单击功能,以便在单击特定 jquery 对话框按钮时运行一些 C# 代码。

我正在使用带有按钮的模式对话框 this jQuery UI 示例。我尝试在 this 中使用 javascript/jquery 回发调用的所有不同答案。问题。

我在 C# 代码中设置了几个断点来查看这些回发是否被调用,但没有任何结果。

我的 ascx 文件中有这个虚拟元素可供使用:

<a id="anchorId" runat="server" onclick="return true;" onserverclick="TryLogin"></a>

我尝试通过几种不同的方式来实现此回发:

$("#anchorId").click(); //just simply does nothing
document.getElementById("anchorId").click(); //This one gives me a null javascript error
$("document").getElementById("#anchorId").click(); //tells me [object] doesn't have a getElementById
__doPostBack('<%=anchorId.UniqueID %>', '');//Also does nothing in the jQuery code, but works in standard javascript code

最后,我尝试检索后面代码中的唯一 ID,如下所示:

string id = anchorId.UniqueID;

并在 JavaScript 中以这种方式替换:

__doPostBack('Softening_Main$anchorId', '');//Still does nothing, and no javascript error

我真的需要一些帮助,有什么想法吗?

最佳答案

您的代码中存在几个问题。

$("document") 不会选择 document 而是会在页面上查找 document 标签元素。您应该使用 $(document) 删除引号。

此外,getElementByIddocument 对象的 JavaScript 方法,用于通过 id 查找元素。当您使用 getElementById 时,请勿在 id 中指定 #。 jQuyer 使用 # 来区分各种选择器。

从 anchor 删除内联 onclick="return true;" 并尝试此操作。

$("#anchorId").click(function(e){
    __doPostBack('<%=anchorId.UniqueID %>', '');
    return false;
});

关于javascript - jquery 回发失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9299265/

相关文章:

javascript - jquery on click事件显示内容

javascript - 图像应使用 jquery 或 javascript 跟随触摸位置

javascript - 如何在 Macbook 的网页上测试捏合事件

javascript - Jquery 无法使用我的代码

jquery - 使用 jquery BlockUI 插件 block 表行

javascript - 如何使用 Angular Translate 翻译页面标题的值?

javascript - 通过 highcharts 在 django 中绘制饼图

JavaScript 函数未完成

javascript - 如何使用 jQuery 将 XML 文件的内容按顺序加载到 HTML 中

jquery - 如何从范围 slider 获取多个值 - bootstrap-slider.js