使用jQuery更新textarea值更改的文本
16lz
2021-01-22
I have a textarea. I need to update text in a div when a value in textarea is changed by either typing in it or by placing some value in it via another jQuery function or pasted.
我有一个textarea。当textarea中的值通过键入或通过另一个jQuery函数放置一些值或粘贴来更改textarea中的值时,我需要更新div中的文本。
$(document).ready(function(){
function myFunc() {
var input = $("#myTxt").val();
$("#txtHere").text(input);
}
myFunc();
// EDIT BELOW -- this will update while typing
$("#myTxt").keyup(myFunc);
});
<textarea id="myTxt"></textarea>
<div id="txtHere"></div>
It loads the value on page load but I'm not sure what to use to check for value in the textarea...
它加载页面加载的值,但我不知道用什么来检查textarea中的值...
7 个解决方案
#1
25
$(document).ready(function(){
function myFunc(){
var input = $("#myTxt").val();
$("#txtHere").text(input);
}
myFunc();
//either this
$('#myTxt').keyup(function(){
$('#txtHere').html($(this).val());
});
//or this
$('#myTxt').keyup(function(){
myFunc();
});
//and this for good measure
$('#myTxt').change(function(){
myFunc(); //or direct assignment $('#txtHere').html($(this).val());
});
});
更多相关文章
- 通过AJAX加载内容和预加载图像?
- jQuery循环没有每个和回调函数
- JavaScript函数中的Ruby代码
- 页面加载后的JQuery(窗口).load?
- 如何将加载微调器图像添加到jquery选项卡中的每个选项卡?
- ajax cache false无法加载图片
- jQuery.parseJSON()函数详解
- 从内部获取函数名称
- Jquery 1.9, JS -函数在Chrome中没有定义