简体   繁体   English

即时搜索功能落后一键

[英]Instant Search function is one keystroke behind

I have a type of "translate" program on my website that uses an instant search function.我的网站上有一种使用即时搜索功能的“翻译”程序。 I have a textarea我有一个文本区域

<textarea name="text" onkeydown="searchq();"></textarea>

which runs the searchq() function运行 searchq() 函数

function searchq() {
    var searchTxt = $("textarea[name='text']").val();

    $.post("translate.php", {text: searchTxt}, function(output){
        $("#translated").html(output);
    });
}

which sends each word to the translate.php将每个单词发送到 translate.php

if(isset($_POST["text"])){
    $words = explode(' ',$_POST['text']);

    $i = 0;
    foreach($words as $word){
        // performs transformations on the words
        $output = $word . " ";
        echo($output);
        $i++;
    }
}

However, if you try it out on my website http://saaa.me/MattSpeak.html , you can clearly see that if you type in "y", nothing shows up, and only when you type in the next letter or space will the "y" show up.但是,如果您在我的网站http://saaa.me/MattSpeak.html上尝试一下,您可以清楚地看到,如果您输入“y”,则不会显示任何内容,并且仅当您输入下一个字母或空格时“y”会出现吗? The instant translation process seems to be one character behind.即时翻译过程似乎落后了一个字符。 What's causing this?这是什么原因造成的? (the Trade Mark signs and "er" word endings are things that I added as part of the "translation") (商标标志和“er”字尾是我在“翻译”中添加的内容)

I have a slight idea of why this isn't working properly but have no clue how to solve it.我对为什么这不能正常工作有一点想法,但不知道如何解决它。 Help would be greatly appreciated.帮助将不胜感激。

The keydown event precedes the character being added (even the keypress event precedes it). keydown事件被添加的字符之前(甚至keypress事件在它之前)。 If you want to respond to a letter being typed, respond to input or, for older and/or buggy browsers (basically IE9 and earlier ), respond to keypress with a brief setTimeout .如果您想响应输入的字母,响应input或者对于较旧和/或有问题的浏览器(基本上是IE9 和更早版本),使用简短的setTimeout响应keypress

Using input :使用input

 $("textarea").on("input", function() { $("<p>").text($(this).val()).appendTo(document.body); });
 <textarea></textarea> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Using keypress with a setTimeout so the character gets added:使用带有setTimeout keypress以便添加字符:

 $("textarea").on("keypress", function() { var $this = $(this); setTimeout(function() { $("<p>").text($this.val()).appendTo(document.body); }, 0); });
 <textarea></textarea> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


I tend to respond to a range of events and use debouncing so I don't respond more than once:我倾向于响应一系列事件并使用去抖动,所以我不会多次响应:

 var lastSearch = null; $("textarea").on("input keyup change", function() { var search = $(this).val(); if (search !== lastSearch) { lastSearch = search; $("<p>").text(search).appendTo(document.body); } });
 <textarea></textarea> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

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

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