Javascript Jquery 填充输入或文本区域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10545573/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:47:56  来源:igfitidea点击:

Jquery to populate input or textarea

javascriptjquery

提问by jahrichie

<div id="example"></div>
  <script type="text/javascript">
             jah = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
             jah+= "<p>Browser Name: " + navigator.appName + "</p>";
             jah+= "<p>Browser Version: " + navigator.appVersion + "</p>";
             jah+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
             jah+= "<p>Platform: " + navigator.platform + "</p>";
             jah+= "<p>User-agent header: " + navigator.userAgent + "</p>";

             document.getElementById("example").innerHTML=jah;

             </script>

I'm using the above code to compile some browser info I need to collect from a form. How can I get that info passed to a Input or Textarea?

我正在使用上面的代码来编译一些我需要从表单中收集的浏览器信息。如何将该信息传递给 Input 或 Textarea?

Thanks in advance!

提前致谢!

采纳答案by XiaoChi

If you want to use pure JavaScript instead of jQuery, try this:

如果你想使用纯 JavaScript 而不是 jQuery,试试这个:

<form><textarea id="ta"></textarea></form>
<script type="text/javascript">
    jah = "whatever you want";
    var ta = document.getElementById('ta');
    ta.value = jah;
</script>

Assigning .valueequals jQuery function .val(v);

赋值.value等于 jQuery 函数.val(v);

回答by rjz

By using $.val. Assuming #exampleis a selector pointing to the input field in question, you can use something like this:

通过使用$.val. 假设#example是一个指向相关输入字段的选择器,您可以使用以下内容:

$('#example').val(jah);

回答by Prasenjit Kumar Nag

just like this using jQuery's valmethod $('#Id').val(jah);

就像这样使用 jQuery 的val方法$('#Id').val(jah);

Where Id is the id of input for textarea. :)

其中 Id 是 textarea 的输入 ID。:)

回答by Mr Coder

$('#myinput').val($('#example').find('p').text());

where 'myinput' is id of your textarea. Paste this line after

其中 'myinput' 是你的 textarea 的 id。将此行粘贴到

document.getElementById("example").innerHTML=jah;