Javascript getElementsByName.value 不起作用

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

Javascript getElementsByName.value not working

javascript

提问by user2583134

I am trying to make a simple javascript program bit it isn't working. Kindly help. In eclipse I have created a dynamic web project and in DD my welcome file is index.jsp. Given below is my code for index.jsp

我正在尝试制作一个简单的 javascript 程序,但它不起作用。请帮助。在 Eclipse 中,我创建了一个动态 Web 项目,而在 DD 中,我的欢迎文件是 index.jsp。下面给出的是我的 index.jsp 代码

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Duncan'S</title>
<script type="text/javascript">
function nameSubmit() {
    alert(document.getElementsByName("username").value);
}
function CakeNumber() {
    alert(document.getElementsByName("numOfCake").value);
}
</script>
</head>
<body>
<form action="myservlet.do">
    <table>
        <tr>
              <td>Name:</td>
              <td><input type="text" id="name" name="username" size="10"
                onchange="nameSubmit();"></td>
        </tr>
        <tr>
              <td>Number Of Duncan's Cake:</td>
              <td><input type="text" id="numOfDunCake" name="numOfCake"
                size="5" onchange="CakeNumber();"></td>
        </tr>
    </table>
</form>
</body>
</html>

In the above code both the functions are returning undefined.....!!How can I get the real value??

在上面的代码中,两个函数都返回 undefined.....!!我怎样才能得到真正的价值??

回答by Praveen

You have mentioned Wrong id

您提到了错误的 ID

alert(document.getElementById("name").value);

if you want to use nameattribute then

如果你想使用name属性那么

alert(document.getElementsByName("username")[0].value);

Updates:

更新:

input type="text" id="name" name="username"  

id is different from name

id 与 name 不同

回答by Allen Chang

document.getElementsByName("name")will get several elements called by same name . document.getElementsByName("name")[Number]will get one of them. document.getElementsByName("name")[Number].valuewill get the value of paticular element.

document.getElementsByName("name")将获得多个同名元素。 document.getElementsByName("name")[Number]将得到其中之一。 document.getElementsByName("name")[Number].value将获得特定元素的值。

The key of this question is this:
The name of elements is not unique, it is usually used for several input elements in the form.
On the other hand, the id of the element is unique, which is the only definition for a particular element in a html file.

这个问题的关键是:
元素的名称不是唯一的,它通常用于表单中的几个输入元素。
另一方面,元素的 id 是唯一的,它是 html 文件中特定元素的唯一定义。

回答by Surya R Praveen

Hereis the example for having one or more checkboxes value. If you have two or more checkboxes and need values then this would really help.

是具有一个或多个复选框值的示例。如果您有两个或更多复选框并需要值,那么这真的很有帮助。

function myFunction() {
  var selchbox = [];
  var inputfields = document.getElementsByName("myCheck");
  var ar_inputflds = inputfields.length;

  for (var i = 0; i < ar_inputflds; i++) {
    if (inputfields[i].type == 'checkbox' && inputfields[i].checked == true)
      selchbox.push(inputfields[i].value);
  }
  return selchbox;

}

document.getElementById('btntest').onclick = function() {
  var selchb = myFunction();
  console.log(selchb);
}
Checkbox:
<input type="checkbox" name="myCheck" value="UK">United Kingdom
<input type="checkbox" name="myCheck" value="USA">United States
<input type="checkbox" name="myCheck" value="IL">Illinois
<input type="checkbox" name="myCheck" value="MA">Massachusetts
<input type="checkbox" name="myCheck" value="UT">Utah

<input type="button" value="Click" id="btntest" />

回答by Yusuf Emre ?akmak

javascript:document.write(document.getElementsByName("signed_request")[0].value);

javascript:document.write(document.getElementsByName("signed_request")[0].value);