我正在尝试启动回发,调用服务器运行元素的单击功能,以便在单击特定 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)
删除引号。
此外,getElementById
是 document
对象的 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/