如何按“进入”键重定向到其他页面

How to press "Enter" to redirect to other page?

本文关键字:其他 重定向 何按 进入      更新时间:2023-09-26

我正在研究搜索功能,我做得很好,得到了我想要的结果。我有一个搜索文本字段,可以让用户键入值,JavaScript将帮助我获得文本字段值的相关数据。现在我想我的文本字段可以功能,当我按下它使用按钮"Enter"。一旦我按下"Enter"按钮,它将重定向到一个新的页面,其中显示了我在文本字段中键入的关键字的所有相关数据。

这是我的JavaScript和HTML代码内的HTML页面:

<script>
$(document).ready(function() {  
    // Icon Click Focus
    $('div.icon').click(function(){
        $('input#search').focus();
    });
    // Live Search
    // On Search Submit and Get Results
    function search() {
        var query_value = $('input#search').val();
        $('b#search-string').html(query_value);
        if(query_value !== ''){
            $.ajax({
                type: "POST",
                url: "search.php",
                data: { query: query_value },
                cache: false,
                success: function(html){
                    $("ul#results").html(html);
                }
            });
        }return false;    
    }
    $("input#search").live("keyup", function(e) {
        // Set Timeout
        clearTimeout($.data(this, 'timer'));
        // Set Search String
        var search_string = $(this).val();
        // Do Search
        if (search_string == '') {
            $("ul#results").fadeOut();
            $('h4#results-text').fadeOut();
        }else{
            $("ul#results").fadeIn();
            $('h4#results-text').fadeIn();
            $(this).data('timer', setTimeout(search, 0));
        };
    });
});
</script>
SEARCH<input type="text" id="search" autocomplete="off">
                    <ul id="results" style="display:none"></ul>

在文本中键入值后,JavaScript将调用search.php来获取所有相关数据。但我不能点击文本框来显示所有值。希望你们能给我一个解决办法。

尝试使用jQuery 1.8或最小。

$("input#search").live("keyup", function(e)
        if(e.which == 13) {
            //perform your operations
        }
    });

你可以把它做成一个表单,这样当用户点击回车键时它就会提交。

<form method="get" action="search page.php" onsubmit="return validate();">
    SEARCH<input type="text" id="search" autocomplete="off">
    <ul id="results" style="display:none"></ul>
</form>
<script type="text/JavaScript">
function validate() {
    //If the form value is "" (nothing)
    if (document.getElementById("search").value == "") {
        return false; //Stop the form from submitting
    }
    return true;
}
</script>