简体   繁体   English

VB.NET:在文本框中按Enter时如何执行jquery代码

[英]VB.NET: How to execute jquery code when pressing enter in textbox

I have an issue where I'm trying to execute a search query from a textbox and attempting to use enter to initiate the search; 我有一个问题,我试图从文本框中执行搜索查询,并尝试使用Enter启动搜索; however, it doesn't seem to be working. 但是,它似乎不起作用。 I'm wondering if vb.net/asp.net is doing a postback or something that I'm not aware of. 我想知道vb.net/asp.net是否正在执行回发或我不知道的事情。

ASPX Page ASPX页面

 <form class="navbar-search">
     <input id="searchbox" type="text" class="search-query span3" placeholder="Search" />
     <div class="icon-search"></div>
 </form>

JQuery JQuery的

$('#searchbox').keyup(function (e) {
    //alert("this will execute");
    if (e.which == 13) {
        alert("this will not execute");
        //window.location.href = "http://www.google.com/";
    }
});

I can't get the alert box to execute or the change location to work... if anyone can help I appreciate it. 如果有人可以帮助我,我将无法执行警报框或更改位置工作。

Try this: 尝试这个:

$("#searchbox").keypress(function (e) {
    //alert("this will execute");
    var code = e.keyCode || e.which;
    if (code == 13) {
        alert("this will not execute");
        //window.location.href = "http://www.google.com/";
    }
});

and to stop page from refreshing on-enter use this: 并要阻止页面刷新进入,请使用以下命令:

$(document).keydown(function (e) {
    var code = e.keyCode || e.which;
    if (code === 13) {
        e.preventDefault();
    }
});

FIDDLE 小提琴

Please try this. 请尝试这个。

$("#searchbox").live("keyup", function(event) {
        if (event.keyCode == 13) {
            //window.location.href = "http://www.google.com/";
        }
    });

This was worked for me... 这对我有用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM