简体   繁体   English

我按键盘上的ENTER键,页面刷新。 如何防止这种情况,仍然使用键盘进行搜索?

[英]I hit the ENTER key on keyboard and page refreshes. How do I prevent this and still use the keyboard to search?

We used to be able to enter a search field, address on search box, hit the ENTER key on the keyboard and get the search results. 我们曾经能够输入搜索字段,搜索框上的地址,按键盘上的ENTER键并获得搜索结果。

I made several changes but can't pinpoint the change that resulted in the ENTER key misbehaving. 我进行了几处更改,但无法查明导致ENTER键行为异常的更改。 Instead of submitting, it refreshes the page. 而不是提交,而是刷新页面。

I have tried each of the following to stop the page refresh: 我尝试了以下每种方法来停止页面刷新:

<form onSubmit="return false;">

<form onkeypress="return event.keyCode != 13">

Each works. 每个工作。

However, I can no longer hit the ENTER key and have results displayed. 但是,我无法再按ENTER键并显示结果。

What do I need to do to fix this? 我需要做什么来解决这个问题?

Below is the js: 以下是js:

function getData()
{

     dijit.byId("advanceSearchDialog").hide(); 
    var form = document.getElementById("searchForm");
    var form2 = document.getElementById("featuresForm")
    var searchText = form.searchBox.value.replace(/-/g,"");
    form.searchBox.value = searchText;

    if (searchText != "") 
    {
        // collect features to search for:
        var features = [ ];
        var featTypes = form2.featType;
        for ( var f = 0; f < featTypes.length; ++f )
        {
            if ( featTypes[f].checked ) features.push( featTypes[f].value );
        }
        featureList = "'" + features.join("','") + "'";

        searchMsg("Searching for '" + searchText + "' ...");
        featureID = "";
        var accord = dijit.byId("accordianContainer");
        var resultsPane = dijit.byId("resultsPane");
        accord.selectChild(resultsPane,true);
        doGlobalSearch( searchText, featureList );
    }
    else
    {
      searchMsg("No search criteria entered, enter search text");
    }   
}


function searchKey(e){
    // special case for IE to capture <enter> in the search box
    var key = window.event ? e.keyCode : e.which;
    var keychar = String.fromCharCode(key);
    if (key == 13)
      getData();
}



<form id="searchForm" class="search_field" method="get" action="">
   <input name="searchBox" id="searchBox" value="" />
  <button type="button" onclick="getData()"><img src="images/magnifying_glass.png" alt="Search" /></button>
</form>

Thanks 谢谢

use just following simple jQuery 使用仅遵循简单的jQuery

if(event.keyCode == 13){ 
   event.preventDefault();
}

...bind it to you form ...将其绑定到您的表格

Whatever function you are calling to display your search results needs to either 您要显示搜索结果的任何函数都需要

  • return false; 返回false;

or 要么

  • call event.preventDefault(); 调用event.preventDefault();

This will avoid the default form action from being executed (causing a full page refresh). 这将避免执行默认的表单操作(导致整页刷新)。

Remove the onkeypress and use onsubmit instead since it is automatically called when the enter key is pressed on an input field of a form. 删除onkeypress并改用onsubmit,因为在表单的输入字段上按Enter键时会自动调用onsubmit。

<form onsubmit="return getData()">
   <input name="searchBox" id="searchBox" value="" />
   <button type="submit"><img src="images/magnifying_glass.png" alt="Search" /></button>
</form>

js JS

function getData() {
    dijit.byId("advanceSearchDialog").hide(); 
    var form = document.getElementById("searchForm");
    var form2 = document.getElementById("featuresForm")
    var searchText = form.searchBox.value.replace(/-/g,"");
    form.searchBox.value = searchText;

    if (searchText != "") 
    {
        // collect features to search for:
        var features = [ ];
        var featTypes = form2.featType;
        for ( var f = 0; f < featTypes.length; ++f )
        {
            if ( featTypes[f].checked ) features.push( featTypes[f].value );
        }
        featureList = "'" + features.join("','") + "'";

        searchMsg("Searching for '" + searchText + "' ...");
        featureID = "";
        var accord = dijit.byId("accordianContainer");
        var resultsPane = dijit.byId("resultsPane");
        accord.selectChild(resultsPane,true);
        doGlobalSearch( searchText, featureList );
    }
    else
    {
      searchMsg("No search criteria entered, enter search text");
    }
    return false;
}

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

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