从表单中的输入字段中获取值的最佳方法是什么?

时间:2017-12-14 14:00:27

标签: javascript html html5 forms

我开始学习JavaScript,但我意识到有很多方法可以从表单中的输入字段中获取值。有比我更多经验的人可以解释实现这一目标最实用(最好)的方法吗?一种方式可能有哪些优点或缺点?如果你能帮助我,我将非常感激。提前谢谢。

function One(){
  var Value = document.forms['FormOne'].Input.value;
  console.log(Value);
}

function Two(){
  var Value = document.FormOne.elements['Input'].value;
  console.log(Value);
}

function Three(){
  var Value = document.getElementById('Input').value;
  console.log(Value);
}

function Four(){
  var Value = document.querySelector('Input').value;
  console.log(Value);
}

function Five(){
  var Value = document.getElementsByName('Input')[0].value
  console.log(Value);
}
<form name="FormOne">
    <input name="Input" id="Input" type="text" size="10"> 
</form>

<br>

<input name="Show" type="Button" onClick="One()" value="Show F-1">
<input name="Show" type="Button" onClick="Two()" value="Show F-2">
<input name="Show" type="Button" onClick="Three()" value="Show F-3">
<input name="Show" type="Button" onClick="Four()" value="Show F-4">
<input name="Show" type="Button" onClick="Five()" value="Show F-5">

0 个答案:

没有答案
相关问题