使用 javascript 或 Jquery 获取 textarea 文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5831413/
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
Get textarea text with javascript or Jquery
提问by antoni
I have an iframe that contains a textarea, like so:
我有一个包含 textarea 的 iframe,如下所示:
<html>
<body>
<form id="form1">
<div>
<textarea id="area1" rows="15"></textarea>
</div>
</form>
</body>
</html>
I want to get the textarea text in the parent page. I've tried this:
我想在父页面中获取 textarea 文本。我试过这个:
var text = $('#frame1').contents().find('#area1').val();
But an empty string is returned. However if I put a value within tags this value is returned successfully:
但是返回一个空字符串。但是,如果我在标签中放置一个值,则该值将成功返回:
<textarea id="area1" rows="15">something</textarea>
How can I get the value of the textarea from the page which contains the iframe?
如何从包含 iframe 的页面获取 textarea 的值?
回答by acdcjunior
READING the <textarea>
's content:
阅读<textarea>
的内容:
var text1 = document.getElementById('myTextArea').value; // plain JavaScript
var text2 = $("#myTextArea").val(); // jQuery
WRITING to the <textarea>
':
写入<textarea>
':
document.getElementById('myTextArea').value = 'new value'; // plain JavaScript
$("#myTextArea").val('new value'); // jQuery
Do notuse .html()
or .innerHTML
!
千万不能使用.html()
或.innerHTML
!
jQuery's .html()
and JavaScript's .innerHTML
should not be used, as they do not pick up changesto the textarea's text.
不应使用jQuery.html()
和 JavaScript .innerHTML
,因为它们不会对 textarea 的文本进行更改。
When the user types on the textarea, the .html()
won't return the typed value, but the original one -- check demo fiddle above for an example.
当用户在 textarea.html()
上键入时,不会返回键入的值,而是返回原始值 - 请查看上面的演示小提琴示例。
回答by rahul
To get the value from a textarea with an id you just have to do
要从带有 id 的 textarea 获取值,您只需要做
Edited
已编辑
$("#area1").val();
If you are having more than one element with the same id in the document then the HTML is invalid.
如果文档中有多个具有相同 id 的元素,则 HTML 无效。
回答by Shawn31313
You could use val()
.
你可以使用val()
.
var value = $('#area1').val();
$('#VAL_DISPLAY').html(value);
回答by dearnotmine
Get textarea text with JavaScript:
使用 JavaScript 获取 textarea 文本:
<!DOCTYPE html>
<body>
<form id="form1">
<div>
<textarea id="area1" rows="5">Yes</textarea>
<input type="button" value="get txt" onclick="go()" />
<br />
<p id="as">Now what</p>
</div>
</form>
</body>
function go() {
var c1 = document.getElementById('area1').value;
var d1 = document.getElementById('as');
d1.innerHTML = c1;
}
回答by Webbu
%100 Solution: $('textarea[name="areaname"]').val()
%100 解决方案: $('textarea[name="areaname"]').val()
回答by Timon. Z
Try .html() instead of .val() :
尝试 .html() 而不是 .val() :
var text = $('#frame1').contents().find('#area1').html();
回答by Diego
As @Darin Dimitrov said, if it is not an iframe on the same domain it is not posible, if it is, check that $("#frame1").contents()
returns all it should, and then check if the textbox is found:
正如@Darin Dimitrov 所说,如果它不是同一域上的 iframe,则不可能,如果是,请检查是否$("#frame1").contents()
返回应有的所有内容,然后检查是否找到了文本框:
$("#frame1").contents().find("#area1").length
should be 1.
$("#frame1").contents().find("#area1").length
应该是1。
Edit
编辑
If when your textarea is "empty" an empty string is returned and when it has some text entered that text is returned, then it is working perfect!! When the textarea is empty, an emptystring is returned!
如果当您的 textarea 为“空”时返回一个空字符串,并且当它输入一些文本时返回该文本,那么它工作完美!!当 textarea 为空时,返回一个空字符串!
Edit 2Ok. Here there is one way, it is not very pretty but it works:
编辑 2好的。这里有一种方法,它不是很漂亮,但它有效:
Outside the iframe you will access the textarea like this:
在 iframe 之外,您将像这样访问 textarea:
window.textAreaInIframe
And inside the iframe (which I assume has jQuery) in the document ready put this code:
在准备好的文档中的 iframe(我假设它有 jQuery)里面放了这个代码:
$("#area1").change(function() {
window.parent.textAreaInIframe = $(this).val();
}).trigger("change");