2

我们以前可以在搜索框中输入搜索字段、地址,然后按键盘上的 ENTER 键并获得搜索结果。

我做了几处更改,但无法确定导致 ENTER 键行为异常的更改。它不是提交,而是刷新页面。

我已尝试以下各项来停止页面刷新:

<form onSubmit="return false;">

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

每个作品。

但是,我不能再按 ENTER 键并显示结果。

我需要做什么来解决这个问题?

下面是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>

谢谢

4

3 回答 3

3

仅使用以下简单的 jQuery

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

...将它绑定到你的表单

于 2013-04-17T13:23:15.863 回答
2

无论您调用什么函数来显示搜索结果都需要

  • 返回假;

或者

  • 调用 event.preventDefault();

这将避免执行默认的表单操作(导致整页刷新)。

于 2013-04-17T13:23:37.283 回答
2

删除 onkeypress 并改用 onsubmit ,因为当在表单的输入字段上按下 enter 键时会自动调用它。

<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;
}
于 2013-04-17T14:55:51.203 回答