最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I hit the ENTER key on keyboard and page refreshes. How do I prevent this and still use the keyboard to search? - S

programmeradmin5浏览0评论

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.

I made several changes but can't pinpoint the change that resulted in the ENTER key misbehaving. 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.

What do I need to do to fix this?

Below is the 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

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.

I made several changes but can't pinpoint the change that resulted in the ENTER key misbehaving. 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.

What do I need to do to fix this?

Below is the 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

Share Improve this question edited Nov 15, 2016 at 1:01 kelvinelove 6201 gold badge8 silver badges19 bronze badges asked Apr 17, 2013 at 13:20 Chidi OkehChidi Okeh 1,5579 gold badges30 silver badges50 bronze badges 3
  • 2 We need to see the code that executes. I would expect onsubmit="return somefunction()" Also event.keyKode is IE only – mplungjan Commented Apr 17, 2013 at 13:23
  • 2 Could you please give us the code of your auto-pletion, or, preferrably, a jsfiddle? – mrks Commented Apr 17, 2013 at 13:23
  • If enter key is refreshing the page, that means your submit handler isn't preventing the default action. – Kevin B Commented Apr 17, 2013 at 14:50
Add a ment  | 

3 Answers 3

Reset to default 6

use just following simple 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;

or

  • call 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.

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

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;
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论