简体   繁体   English

如何从JavaScript函数中的HTML输入读取文本?

[英]How to read text from HTML input in JavaScript function?

On the webpage, the user is able to write text in a text field. 在网页上,用户可以在文本字段中编写文本。 I would like to use the button on this page to read this text and then place it somewhere else. 我想使用此页面上的按钮阅读此文本,然后将其放置在其他位置。 Here is the simplified markup so far: 到目前为止,这是简化的标记:

<div id="Demo1" class="w3-accordion-content w3-container">
<h4>Let's make your Resume Header</h4>
<table>
  <tr>
    <th>Name</th>
    <td><input type="text" id="name"></td>
  </tr>
</table>
<button onclick="header()" class="save">SAVE</button>
</div>

And here is the JavaScript code that I have: 这是我拥有的JavaScript代码:

function header() {
  var name = document.getElementById(name).value;
  alert(name);
}

This alert does not work at all when I run it. 运行此警报时,它根本不起作用。 I cannot figure out why, any help is much appreciated. 我不知道为什么,非常感谢您的帮助。

You must wrap the elementById argument in quotes, as well as have a way for your HTML to reference your JavaScript. 您必须将elementById参数用引号引起来,并且必须为HTML引用JavaScript提供一种方法。 See Below. 见下文。

<script>
function header() {
  var name = document.getElementById("name").value;
  alert(name);
}
</script>

<div id="Demo1" class="w3-accordion-content w3-container">
<h4>Let's make your Resume Header</h4>
<table>
  <tr>
    <th>Name</th>
    <td><input type="text" id="name"></td>
  </tr>
</table>
<button onclick="header()" class="save">SAVE</button>
</div>

You have missed quotes. 您错过了报价。 So use document.getElementById('name').value instead of document.getElementById(name).value . 因此,请使用document.getElementById('name').value代替document.getElementById(name).value

if you use name as a parameter then you can use it without quotes. 如果使用name作为参数,则可以使用不带引号的名称。

 function header() { var name = document.getElementById('name').value; alert(name); } 
 <div id="Demo1" class="w3-accordion-content w3-container"> <h4>Let's make your Resume Header</h4> <table> <tr> <th>Name</th> <td> <input type="text" id="name"> </td> </tr> </table> <button onclick="header()" class="save">SAVE</button> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将输入(type = text)从html表单传递给javascript函数 - how to pass input(type = text ) from a html form to javascript function 如何从javascript中的输入文件中读取文本? - how to read text from input file in javascript? 将HTML中的文本表单值输入到Javascript函数中 - Input text form value from HTML into Javascript function HTML/Javascript - 如何将输入字段中的文本显示为 HTML 代码? - HTML/Javascript - How to display text from input field as HTML code? HTML如何基于javascript函数的返回值限制文本输入? - How is HTML restricting text input based on the return value of a javascript function? 如何在Javascript中不显眼地从按钮点击的文本中读取输入? - How to read input from text on button click unobtrusively in Javascript? 如何使用 Javascript 从以下 HTML 中读取文本“LE 88” - How to read text "LE 88" from the following HTML using Javascript 将HTML文本输入值获取到Javascript函数中 - Get Html text input value into a Javascript function 如何使用JavaScript从用户输入中读取F(x)函数? - How to read F(x) function from user input with JavaScript? 如何交换 HTML JavaScript 中两个文本字段的输入? - How to swap the input from two text fields in HTML JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM