[英]Dynamic text in textarea - getting value
My text/value in textarea it's not static - I'm chaning it. 我在textarea中的文本/值不是静态的-我正在更改它。 I can't get the current value. 我无法获得当前值。 Eg 1 例如1
<textarea>
Lorem ipsum
</textarea>
//it's defalut in html file
2 2
Putting into textarea: Dolores is lorem ipsum
Alert is only showing 1 version("lorem ipsum"), but not second ("Dolores is lorem ipsum"). 警报仅显示1个版本(“ lorem ipsum”),而不显示第二个版本(“ Dolores is lorem ipsum”)。 I'm trying to do it in jquery: 我正在尝试在jquery中做到这一点:
var variable = $("#selector").val();
alert(variable);
What I'm doing wrong? 我做错了什么?
I want to catch it to variable :) Not to alert. 我想将其捕获到变量中:)不提醒。 Alert is only my test :) 警报只是我的测试:)
$('textarea').change(function() {
alert($(this).val());
});
Try to tie the alert()
to an event: 尝试将alert()
与事件关联:
$('textarea').change(
function(){
alert($(this).val());
});
References: 参考文献:
var text = $('#textareaID').val();
$('#textareaID').change(function() {
text = $(this).val();
});
when ever you want text just reference it :) 每当您想要文本时,只需引用它即可:)
EDIT: 编辑:
If your using tabs UI please review the docs and the event management: 如果您使用的是标签用户界面,请查看文档和事件管理:
Place This outside of bound scope:
var text = $('#textareaID').val(); OR var text = '';
$('#example').bind('tabsselect', function(event, ui) {
// Objects available in the function context:
// ui.tab anchor element of the selected (clicked) tab
// ui.panel element, that contains the selected/clicked tab contents
// ui.index zero-based index of the selected (clicked) tab
// INSIDE HERE IS WHERE YOU CAN PUT THE CODE IN THE ABOVE EXAMPLE
$('#textareaID').change(function() {
text = $(this).val();
});
});
NOTE: $('#example') would be the parent div that holds the tabs and content 注意:$('#example')将是保存选项卡和内容的父div
Further optimization recommendation. 进一步的优化建议。
If you think $('#textareaID') will be called often you may want to cache a reference to it so the selector engine does not have to find it on every instance, this would be done like: 如果您认为$('#textareaID')通常会被调用,则您可能希望缓存对其的引用,以便选择器引擎不必在每个实例上都找到它,可以这样进行:
var textarea = $('#textareaID');
var text = $('#textareaID').val();
For this line: 对于此行:
var textarea = $('#textareaID');
Make sure it is inside of a $(document).ready(function() {}); 确保它在$(document).ready(function(){});内部; Call and the element is exists 呼叫且元素存在
you could check for this by doing: 您可以通过以下方法进行检查:
var textarea = $('#textareaID') || false;
And wrap the code above like this: 并像上面那样包装上面的代码:
$('#example').bind('tabsselect', function(event, ui) {
// Objects available in the function context:
// ui.tab anchor element of the selected (clicked) tab
// ui.panel element, that contains the selected/clicked tab contents
// ui.index zero-based index of the selected (clicked) tab
// INSIDE HERE IS WHERE YOU CAN PUT THE CODE IN THE ABOVE EXAMPLE
if(textarea) {
textarea.change(function() {
text = $(this).val();
});
}
});
Hope this helps! 希望这可以帮助!
It sounds like you are trying to assign the .val()
to a variable before the value changes. 听起来您正在尝试在值更改之前将.val()
分配给变量。 You are correct in that you want to use .val()
. 您想使用.val()
是正确的。 Try this jsfiddle to see the differences that are produced by the variable
, .val()
, .text()
, and .html()
. 尝试这种的jsfiddle地看到,由所产生的差异variable
, .val()
.text()
和.html()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.