JQUERY 在键入时将文本框的内容复制到字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6160076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:29:22  来源:igfitidea点击:

JQUERY copy contents of a textbox to a field while typing

jquery

提问by Sujit Agarwal

I am trying to copy the contents of a textbox into a div simultaneously while the user is typing. Here is THE CODE ON JSFIDDLEThe error that is am facing is, the length of the value copied inside the div is always one less than that of the textbox. what error am i making in the script?

我正在尝试在用户键入时同时将文本框的内容复制到 div 中。这是JSFIDDLE 上代码我面临的错误是,在 div 内复制的值的长度总是比 textbox 的值少一。我在脚本中犯了什么错误?

回答by Guidhouse

Use keyupinstead.

使用keyup来代替。

$("#boxx").keyup(function(event) {
  var stt = $(this).val();
  $("div").text(stt);
});

keypressoccurs when the key is pressed down and you want the text transferred when the key is released.

keypress当按键被按下并且您希望在松开按键时传输文本时发生。

回答by Rahatur

The keyupand keypressevents work for keyboard input, but if one uses the mouse to right-click and paste something into the text box then the value change will not be picked up. You can use bindwith the inputevent to register both keyupand paste events like this:

KEYUP按键事件工作的键盘输入,但如果使用鼠标右键单击并粘贴东西到文本框,然后将值变化不会被拾起。您可以使用bind输入事件注册这两个KEYUP并贴上这样的事件:

$("#textbox1").bind('input', function () {
   var stt = $(this).val();
   $("#textbox2").val(stt);
});

回答by Frédéric Hamidi

The keypressevent occurs before the text in the <input>element is updated. You can delay the copy operation to work around that. Even a 0 millisecond delay will be enough for the copy operation to occur after the element is updated:

keypress事件在<input>元素中的文本更新之前发生。您可以延迟复制操作来解决这个问题。即使是 0 毫秒的延迟也足以在元素更新后发生复制操作:

$("#boxx").keypress(function() {
    var $this = $(this);
    window.setTimeout(function() {
       $("div").text($this.val());
    }, 0);
});

Updated fiddle here.

在这里更新小提琴。

回答by R T

$("#title").keypress(function() {
                var $this = $(this);
                window.setTimeout(function() {

                   $("#slug-url").val($this.val().toLowerCase().replace(/ /g, '-'));
                }, 0);
            });

using @Frédéric Hamidi' method to generate seo friendly url after replacing spaces with '-' and changing text to smallcase.

使用@Frédéric Hamidi' 方法在用'-' 替换空格并将文本更改为小写后生成seo 友好的url。

回答by Muhammad Tahir

use keyup and change both.

使用 keyup 并更改两者。

$("#boxx").on('keypress change', function(event) {
       var data=$(this).val();
       $("div").text(data);
});

here is the example http://jsfiddle.net/6HmxM/785/

这是示例 http://jsfiddle.net/6HmxM/785/

回答by PINAKI SHANKAR

$("#boxx").keyup(function() {
    var $this= $(this);
    window.setTimeout(function() {
       $("div").text($this.val());
    }, 0);
});

this work proper.

这个工作得当。

Copy also works f9enter code here

复制也适用 f9enter code here