【问题标题】:docment.forms["myForm"]["Element"].value command in js form validationjs表单验证中的document.forms["myForm"]["Element"].value命令
【发布时间】:2012-07-23 14:31:08
【问题描述】:

我正在做一个简单的 Javascript 表单验证,但无法访问表单中的元素。似乎命令:

document.forms["myForm"]["un"].value;

适用于 Explorer,但不适用于 Firefox。我正在处理的表格是:

 <form name = "myForm" onsubmit="check()" method="POST">
            User Name:<br><input type="text" name = "un"><br/>
    Password:<br><input type="password" name = "pw"><br/>
    Confirm Password:<br><input type="password" name = "cpw"><br/>
            Email:<br><input type = "text" name = "em"><br/>
    <input type = "submit" id = "sub" value="submit">

让js验证成为:

<script type = "text/javascript">
var user = document.forms["myForm"]["un"].value;
alert(user);
</script>

【问题讨论】:

  • 欢迎使用 stackoverflow。你有什么问题?
  • @DanielA.White 真的吗?通常我对格式不正确的问题很苛刻,但我认为这个问题已经足够清楚了。
  • 使用 document.forms["myForm"].elements["un"].value
  • var user = document.getElementById("un").value; &lt;input type="text" name = "un" id='un'&gt;.

标签: javascript forms validation


【解决方案1】:

试试这个,

var user = document.myForm.un.value;

我不确定您是否已经听说过 JQuery,但我建议您使用它。看看这个http://jquery.com/

【讨论】:

  • 为什么要推荐 jQuery?
  • @sachleen,我认为他只是 javascript 的新手,他实际上可以查看 jQuery 库并利用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-15
  • 2022-11-11
  • 1970-01-01
  • 2020-08-07
  • 1970-01-01
相关资源
最近更新 更多