Javascript 如何使用javascript在IE中的表单中设置文本区域值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5589057/
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
How to use javascript to set text area value in a form in IE
提问by user570494
I can use this to set a text area (selectedtext) value in a form (submitquestion) if Firefox, but it fails in IE.
如果 Firefox,我可以使用它在表单(提交问题)中设置文本区域(selectedtext)值,但它在 IE 中失败。
document.submitquestion.selectedtext.value = txt;
document.submitquestion.selectedtext.value = txt;
回答by ysrb
This should work:
这应该有效:
<textarea id="bla">from</textarea>
<script type="text/javascript">
document.getElementById("bla").value = "test";
</script>
回答by moe
Try this:
尝试这个:
document.forms['submitquestion'].elements['selectedtext'].value = txt;
Assuming you have:
假设你有:
<form name='submitquestion'>
<textarea name='selectedtext'></textarea>
</form>
回答by Richard Schneider
I recommend using JQuery, it works with all browsers.
我推荐使用 JQuery,它适用于所有浏览器。
$('#selectedtext').val('whatever');
回答by Smith
You can do this in pure javascript like this
你可以像这样在纯 javascript 中做到这一点
document.getElementById("myTextarea").value = txt;