Javascript 获取 html 输入标签值

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

Get html input tag value

javascripthtml

提问by Upperflow

Why this code doesn't work? is "id" attribute required?

为什么这段代码不起作用?是否需要“id”属性?

<html>
<head>
<script language="JavaScript">
  function pprint(){
   var a = tf.value;
   document.write(a);
     }
 </script>
</head>
<body>
<input type="text" name="tf"  value="Test">
<input type="button" onClick="pprint()" value="print" >
</body>
</html>

Thanks!

谢谢!

回答by Achrome

In a form, the nameattribute is required to fill the $_POSTor $_GETglobal variables with a (name, value) pair.

在表单中,name属性需要用 ( , ) 对填充$_POST$_GET全局变量。namevalue

Javascript will not identify an element from only the name. So, you would be better off using the idattribute.

Javascript 不会仅从名称识别元素。因此,您最好使用该id属性。

<input type="text" name="tf" id="tf"  value="Test">

And in your Javascript, you can directly call document.getElementById('tf')to get the element.

而在你的 Javascript 中,你可以直接调用document.getElementById('tf')来获取元素。

Something like this.

像这样的东西。

document.write(document.getElementById('tf').value);

回答by Lee Meador

Two changes. 'tf' is an 'id' and get the element in the javascript. And also 'alert' as suggested above.

两个变化。'tf' 是一个 'id' 并在 javascript 中获取元素。还有上面建议的“警报”。

<html>
<head>
<script language="JavaScript">
  function pprint(){
   var tf = document.getElementById('tf');
   var a = tf.value;
   alert(a);
  }
 </script>
</head>
<body>
<input type="text" id="tf"  value="Test">
<input type="button" onClick="pprint()" value="print" >
</body>
</html>

回答by MrCode

Why this code doesn't work?

为什么这段代码不起作用?

Javascript doesn't give you a ready to use handle on the element like you're expecting. You'll have to get the element by its name, ID, tag name or through its parent form (if it had one).

Javascript 并没有像您期望的那样为您提供随时可用的元素句柄。您必须通过其名称、ID、标签名称或通过其父表单(如果有)来获取元素。

is "id" attribute required?

是否需要“id”属性?

No, the ID is not required to get a handle on the element. Example of getting the element by name:

不,获取元素句柄不需要 ID。按名称获取元素的示例:

function pprint(){
    var a = document.getElementsByName("tf")[0].value;
    console.log(a);
}

回答by Nishu Tayal

While using JavaScript, if you want to access any element, you can choose to use:

在使用 JavaScript 时,如果要访问任何元素,可以选择使用:

  1. document.getElementById (id)
  2. document.getElementByName] (name)
  1. document.getElementById (id)
  2. document.getElementByName](名称)

You can use following code:

您可以使用以下代码:

<script language="JavaScript">
  function pprint(){
    // If you want to use element Id
      var a = document.getElementById('tf').value;
      document.write(a);
    // If you want to access by element name
      var b = document.getElementsByName('tf')[0].value;
      document.write(b);
 }
 </script>

Working demo on JsFiddle

JsFiddle 上的工作演示

  1. document.getElementById on W3 Schools
  2. document.getElementByName on W3 Schools
  1. W3 学校上的 document.getElementById
  2. W3 学校上的 document.getElementByName