[英]Prepending input value to textarea in jQuery
I'm trying to write a script that prepends the contents of a text input to a textarea in real time 我正在尝试编写一个脚本,该脚本实时将文本输入的内容添加到文本区域
Here's what I currently have: 这是我目前拥有的:
<textarea rows="4" id="tweet-text" name="tweet-text"></textarea>
<input id="prepend" name="prepend" type="text">
jQuery: jQuery的:
function appendTweet()
{
var tweet = $("#tweet-text").val();
$('#prepend').bind('keyup keypress', function() {
$('#tweet-text')[0].value = tweet + $(this)[0].value;
});
}
jQuery(document).ready(function($)
{
appendTweet();
$('#prepend').keyup(appendTweet);
});
Currently my input is the previous string plus the new one, so if I typed hello I'd get 目前,我的输入是前一个字符串加上新的字符串,因此如果输入hello,我会得到
h
H
hhe
he
hehel
hel
helhell
赫尔
hellhello
你好
I can see why this is happening, but I can't find the solution. 我知道为什么会发生这种情况,但找不到解决方案。
EDIT 编辑
To clarify, I'm looking for the following outcome: 为了澄清,我正在寻找以下结果:
#prepend
. #prepend
输入“ test”。 #tweet-text
now says 'test'. #tweet-text
现在显示“测试”。 #tweet-text.
#tweet-text.
输入“ test2” #tweet-text.
#tweet-text` now says 'test test2'. #prepend
so that it now says 'test3 '. #prepend
,使其现在显示为'test3'。 #tweet-text
now says 'test3 test2'. #tweet-text
现在显示“ test3 test2”。 Try this: 尝试这个:
jQuery(document).ready(function($) {
$('#prepend').onkeydown(function() {
var tweet = $("#tweet-text").val();
$('#prepend').val(tweet);
});
});
do like this: 这样做:
function appendTweet()
{
var tweet = $("#prepend").val();
$('#tweet-text').val(function(index, old) { return tweet + old; });
}
This will prepend the textbox text in text area. 这将在文本区域中的文本框文本之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.