【发布时间】:2014-06-17 07:35:15
【问题描述】:
我希望在按下回车按钮时激活链接 <a href='#' onclick='function()'> 的 onclick 事件。这应该可以工作,但我似乎无法正常工作。 Trigger a button click with JavaScript on the Enter key in a text box 。这也不适用于按钮。
原因可能是因为加载问题,我使用 ajax 运行和加载大部分界面,某些计算需要相当长的时间,所以我想我应该使用 ajax 来显示我的页面元素,这样我就可以更新真实的时间而不是让用户等待服务器 5 到 10 秒。然而,这似乎使下面的代码不起作用。
我的代码
//This comes from a different php file which is read out on the server with
//readfile() in php after an ajax call. In other words it echoes the
//contents of this file on the page including this link.
echo "<a href='#' onclick='dosomething()' ID='Enter'>Click me</a>";
//js
$("#Enter").keyup(function(event){
if(event.keyCode == 13){
$("#Enter").click();
}
});
function dosomething(){
//activate some code depends on the link.
}
那么我该如何让它发挥作用。在通过ajax导入的链接上激活onclick on enter?这甚至可能吗?
【问题讨论】:
标签: javascript php jquery html ajax