html< a>点击后激活按下输入 [英] html <a> activate on click on enter pressed

查看:163
本文介绍了html< a>点击后激活按下输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望按下输入按钮时激活链接< a href ='#'onclick ='function()'> 的onclick事件。这应该可以工作,但我似乎没有让它工作。 触发按钮点击使用JavaScript在文本框中的Enter键。这也不适用于按钮。

I want the onclick event of a link <a href='#' onclick='function()'> to activate when the enter button is pressed. This is supposed to work, but I don't seem to get it working. Trigger a button click with JavaScript on the Enter key in a text box . And this also didn't work for a button.

原因可能是因为我因为加载问题而运行并加载了大部分接口ajax,计算需要相当长的时间,所以我想我应该使用ajax来显示我的页面元素,这样我就可以更新实时而不是让用户等待服务器5到10秒。但是这似乎使下面的代码无效。

The reason for this is probably because I run and load most of my interface in with ajax because of loading issues, certain calculations take quite some time, so I figured I should use ajax to show my page elements so I can update real time instead of making the user wait 5 to 10 seconds for the server. This however seems to make the code below not working.

我的代码

//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导入的链接上激活oncl​​ick on enter?这甚至可能吗?

So how do I get this working. Activating the onclick on enter pressed on a link imported via ajax? Is this even possible?

推荐答案

工作演示

Working Demo

试试这个,

使用 keypress 事件,它不会在 #Enter 上触发,因为它是< a> tag。

Use keypress event and it won't fire on #Enter as it is an <a> tag.

$(document).keypress(function(event){

    if(event.keyCode == 13){
        $("#Enter").click();   //OR $("#Enter").trigger('click');
    }
});

function dosomething(){
    alert('..');
    //activate some code depends on the link.
}

这篇关于html&lt; a&gt;点击后激活按下输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆