简体   繁体   English

HTML表单中的键盘友好AJAX搜索

[英]Keyboard friendly AJAX search within an HTML form

I have a form that looks something like this: 我有一个看起来像这样的表格:

<form action="whatever.php" method="post">

    <input type="text" onKeyUp="ajax_search()" name="search" id="search">

    // THE AJAX FUNCTION UPDATES THE DIV BELOW
    <div id="search_results"></div>

    <input type="text" name="price" id="price">
    <input type="text" name="cost" id="cost">
    <input type="text" name="discount" id="discount">

    <input type="submit" value="GO">

</form>

I have an AJAX function ( ajax_search() ) that points to a PHP file that returns items from a database table that are similar to what is input in the search input. 我有一个AJAX函数( ajax_search() ),该函数指向一个PHP文件,该文件从数据库表中返回与search输入中所输入内容相似的项目。 ( ... WHERE code LIKE '"$code"%' ) ... WHERE code LIKE '"$code"%'

No problems so far. 到目前为止没有问题。 The items returned are done so in the following format: 返回的项目以以下格式完成:

$output .= '<p onClick="insert_item(' .json_encode($code). ')">' .$code. '</p>';

When the <p> tag is clicked, the <form> is updated with the item ( $code ) and all it's relative information. 单击<p>标记时,将使用项目( $code )及其所有相关信息来更新<form> This works great! 这很棒!

I am now looking to add in keyboard functionality to the AJAX search. 我现在正在寻找将键盘功能添加到AJAX搜索中的功能。 The user should be able to 'select' the items using the keyboard arrows. 用户应该能够使用键盘箭头“选择”项目。

I read a similar discussion here: keyboard friendly AJAX search 我在这里阅读了类似的讨论: 键盘友好的AJAX搜索

The discussion doesn't go very far and the end result was that the code did not work for the OP. 讨论进行得并不多,最终结果是该代码不适用于OP。

Also, because the search_results div is displayed amongst a <form> already and the <input id="search"> is focused, the 'ENTER' key will activate the submit button instead of calling the insert_item() function. 另外,由于search_results div已显示在<form> ,并且<input id="search">已被聚焦,因此'ENTER'键将激活submit按钮,而不是调用insert_item()函数。 I may be able to sort this issue out on my own using javascript if() statements and ASCII codes. 我可以使用javascript if()语句和ASCII代码自行解决此问题。

Please help me in selecting the searched items using keyboard arrow keys? 请帮助我使用键盘箭头键选择搜索到的项目吗?

UPDATE UPDATE

I have tried giving the serached <p> items an <a> tag for tab selection. 我曾尝试为搜索的<p>项目提供<a>标签以供标签选择。 But when I hit tab , all that happens is the next <input> in the <form> is focused. 但是,当我点击tab时 ,所有发生的是<form>的下一个<input> <form>被聚焦。

I would catch the charCodes in an if statement something like this: 我会在if语句中捕获charCodes,如下所示:

$("#search").on("keyup", function(e) {
    if(e.charCode === 38) {  // Up arrow
        e.preventDefault(); // Prevent the default action
        currentSelection++; // Update the current selection
    } else if(e.charCode === 40) { // Down Arrow
        // ...etc....
    }
});

Theres a pretty helpful list of the charCodes here . 即使世界的charCodes的非常有益的列表在这里

You should be able to catch any of they keys this way (tab, enter, arrows etc..) and prevent their actions using e.preventDefault() 您应该能够以这种方式捕获它们的任何键(制表符,回车键,箭头等),并使用e.preventDefault()阻止其动作。

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

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