简体   繁体   English

读取Javascript中的表单字段数据

[英]Reading form field data in Javascript

I just can't get this to work. 我只是无法使它正常工作。

HTML: HTML:

<form action="index.php?page=n0402" method="post" Name="AddPolish" >
    <div id="frmBrandInput">
        <label for="frmBrand">Brand name:</label>
        <input type="text" name="frmBrand" size="50" onkeypress="BrandCheck();" maxlength="100" id="frmBrand" value="Enter existing or new brand"  />
        <span id="frmBrand_Status"></span>
    </div>
</form>

Javascript: Javascript:

function BrandCheck()
{
    var jsBrandName = document.forms["AddPolish"]["frmBrand"].value;
    alert(jsBrandName);
}

Why can't I get the value of frmBrand into jsBrandName? 为什么不能将frmBrand的值转换为jsBrandName? If I use Firebug to debug, I get the following in my Watch list: Watch Expression: 如果使用Firebug进行调试,则会在“监视”列表中获得以下内容:监视表达式:

document.forms["AddPolish"]["frmBrand"];

Result: 结果:

NodeList[input#frmBrand property value = "G" attribute value = "Enter existing or new brand", input#frmBrand attribute value = ""]

I can see the value "G" which is what I entered in the input field. 我可以看到在输入字段中输入的值“ G”。 What am I doing wrong in passing it into the jsBrandName? 将其传递给jsBrandName时我做错了什么?

The output you got implies that there are two inputs with name="frmBrand" in the form. 您得到的输出暗示在表单中有两个name="frmBrand"输入。 You need to index them, eg 您需要索引它们,例如

var jsBrandName = document.forms["AddPolish"]["frmBrand"][0].value;

to get the value of the first one. 得到第一个的价值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM