简体   繁体   English

如果按下键,则运行JavaScript!

[英]Run JavaScript if key pressed != ENTER

Im working on a MVC4 site, and I have a webgrid with a search textbox. 我在MVC4网站上工作,我有一个带有搜索文本框的网络网格。 My Textbox is inside a form, which will be submitted when I press enter. 我的文本框位于表单中,当我按Enter时将提交该表单。 I also have a onkeypress script bound to the textbox, that will, after 3 sek, update my webgrid with what else is entered. 我还有一个绑定到文本框的onkeypress脚本,它将在3秒钟后用其他输入内容更新我的Webgrid。

My problem is, that I only want to run the script if not the last key pressed is Enter. 我的问题是,如果不是最后一个按下的键是Enter,我只想运行脚本。

My code looks like this: 我的代码如下所示:

                    @using (Ajax.BeginForm("Filter", new AjaxOptions { HttpMethod = "GET", InsertionMode = InsertionMode.Replace, UpdateTargetId = "projects" }))
                {  
                    <div class="paddingTextToleft">
                        Search:
                        <input id="searching" name="searchString" type="text" value="" onkeypress="return keypressed()">


                        <p class="error">@ViewBag.SearchMessage</p>

                    </div>
                    <br />
                }

And the script: 和脚本:

 var timeoutReference;

function keypressed() {
    if (window.event.keyCode == 13) {
        //Do not run the script!
        return true;
    }
    else {
        if (timeoutReference) clearTimeout(timeoutReference);
        timeoutReference = setTimeout(function () {
            var value = $("#searching").val();


            $.ajax({
                url: '@Url.Action("Filter", "Project")',
                contentType: 'application/html; charset=utf-8',
                type: "GET",
                dataType: 'html',
                data: { searchString: value },
            }).success(function (result) {
                $('#projects').html(result);
            });
        }, 3000);
    }
};

I want it to stop the script (or not run the rest of it), if the key pressed is enter. 如果要输入按下的键,我希望它停止脚本(或不运行其余脚本)。

Hope anyone can help me. 希望任何人都能帮助我。

Thanks 谢谢

Firstly you are not sending Event to the function. 首先,您没有将Event发送给函数。 Call it with some parameters eg: 用一些参数调用它,例如:

<input id="searching" name="searchString" type="text" value="" onkeypress="keypressed(e)">

Then accept this event in a function: 然后在函数中接受此事件:

var timeoutReference;

function keypressed(e) {
if (e.keyCode == 13) {
    //Do not run the script!
    return;
}
else {
    if (timeoutReference) clearTimeout(timeoutReference);
    timeoutReference = setTimeout(function () {
        var value = $("#searching").val();


        $.ajax({
            url: '@Url.Action("Filter", "Project")',
            contentType: 'application/html; charset=utf-8',
            type: "GET",
            dataType: 'html',
            data: { searchString: value },
        }).success(function (result) {
            $('#projects').html(result);
        });
    }, 3000);
}

}; };

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

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