输入时jQuery自动完成重定向 [英] jQuery autocomplete Redirection upon enter

查看:61
本文介绍了输入时jQuery自动完成重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个完美无缺的自动完成功能,但我无法确定如何将用户重定向到一个单独的网页,其中包含按键盘上的ENTER选项信息。网站本身不一定存在,我只想知道网站已经存在的代码。

I have an Autocomplete that works perfectly but I'm having trouble figuring out how to re-direct the user to a separate web page containing info about his selection upon pressing ENTER on keyboard. The website itself doesn't have to exist, I just want to know the code for it assuming the website already exists.

index.php:

index.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Autocomplete using PHP/MySQL and jQuery</title>
<link rel="stylesheet" href="css/style.css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
</head>


<body>
    <div class="container">
        <div class="header">

        </div><!-- header -->
        <h1 class="main_title">Autocomplete using PHP/MySQL and jQuery</h1>
        <div class="content">
            <form>
                <p>Table consists of : ID, Location, Slug, Population </p>
                <br><br>
                <div class="label_div">Search for a Slug : </div>
                <div class="input_container">
                    <input type="text" id="slug" onkeyup="autocomplet2()">
                    <ul id="list_id"></ul>
                </div>
            </form>
            <br><br><br><br>
            <p>List will be ordered from Highest population to lowest population (Top to bottom)</p>
                <br><br>
        </div><!-- content -->    
        <div class="footer">
            Powered by Jason's Fingers</a>
        </div><!-- footer -->
    </div><!-- container -->
</body>
</html>

script.js:

script.js:

// autocomplete : this function will be executed every time we change the text
function autocomplet2() {
    var min_length = 3; // min caracters to display the autocomplete
    var keyword = $('#slug').val();
    if (keyword.length >= min_length) {
        $.ajax({
            url: 'ajax_refresh.php',
            type: 'POST',
            data: {keyword:keyword},
            success:function(data){
                $('#list_id').show();
                $('#list_id').html(data);
            }
        });
    } else {
        $('#list_id').hide();
    }
}

// set_item : this function will be executed when we select an item
function set_item(item) {
    // Changes input to the full name on selecting
    $('#slug').val(item);
    // Hides list after selection from list
    $('#list_id').hide();
}

function change()

ajax_refresh.php:

ajax_refresh.php:

<?php
// PDO connect *********



function connect() {
    return new PDO('mysql:host=localhost;dbname=wallettest', 'root', 'butthead', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}

$pdo = connect();
$keyword = '%'.$_POST['keyword'].'%';
$sql = "SELECT * FROM population WHERE slug LIKE (:keyword) ORDER BY population DESC LIMIT 0, 10";
$query = $pdo->prepare($sql);
$query->bindParam(':keyword', $keyword, PDO::PARAM_STR);
$query->execute();
$list = $query->fetchAll();
foreach ($list as $rs) {
    // put in bold the written text
    $slug = str_replace($_POST['keyword'], '<b>'.$_POST['keyword'].'</b>', $rs['slug']);
    // add new option
    echo '<li onclick="set_item(\''.str_replace("'", "\'", $rs['slug']).'\')">'.$slug.'</li>';
}


?>


推荐答案

收听 keydown event,并通过检查键码来检查按下的键是否是回车键。

Listen for the keydown event, and check if the key pressed was the enter key, by checking the keycode.

input.on('keydown', function (ev) {
  if (ev.keyCode === 13) { //enter's keycode is 13
    //redirect code
  }
});

你可能还需要做 ev.preventDefault()如果输入键通常会做不需要的事情。

You might also have to do ev.preventDefault() if the enter key would normally do something unwanted.

这篇关于输入时jQuery自动完成重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆