访问表单名称值的Javascript问题

时间:2009-06-10 21:20:50

标签: javascript

我想通过使用document.write(document.getElementById('listing')。name);

来访问我拥有的表单的'name'值。

这不会输出任何奇怪的东西。更奇怪的是以下内容。

当我摆脱box4下面的所有内容时,它只会带回“NordstromListing”。我完全不知道为什么会这样。如果有人能够指出我做错了什么,我会很感激。

谢谢!

<form action="NordstromListing.php" id="listing" name="NordstromListing" method="POST" onSubmit="return validate_form ( );">

            <label>Enter Item Number:&nbsp;</label> <input type="text" size="15" name="itemnum" />
            <p>Verify Item Number: <input type="text" size="15" name="itemnumverify" /></p>

            <label>MSRP Price: &nbsp; $ &nbsp; <input type="text" size="15" id ="msrp" name="msrp" onBlur="discount()" onKeyup="discount()" onChange="discount(.90)"></input>
                <br />
            <label>Discounted Price: $ <input readonly type="text" size="15" id="edprice" name="edprice"></input>
                <br />

            <label>Item Category: </label><select name="itemtype" class="itemcat" onchange="if (this.selectedIndex==1){this.form['box'].style.visibility='visible'}else {this.form['box'].style.visibility='hidden'}; 
            if (this.selectedIndex==2){this.form['box2'].style.visibility='visible'}else {this.form['box2'].style.visibility='hidden'};
            if (this.selectedIndex==2){this.form['box25'].style.visibility='visible'}else {this.form['box25'].style.visibility='hidden'};
            if (this.selectedIndex==3){this.form['box3'].style.visibility='visible'}else {this.form['box3'].style.visibility='hidden'};
            if (this.selectedIndex==4){this.form['box4'].style.visibility='visible'}else {this.form['box4'].style.visibility='hidden'};
            if (this.selectedIndex==5){this.form['box5'].style.visibility='visible'}else {this.form['box5'].style.visibility='hidden'};
            if (this.selectedIndex==6){this.form['box6'].style.visibility='visible'}else {this.form['box6'].style.visibility='hidden'};
            if (this.selectedIndex==7){this.form['box7'].style.visibility='visible'}else {this.form['box7'].style.visibility='hidden'};
            if (this.selectedIndex==8){this.form['box8'].style.visibility='visible'}else {this.form['box8'].style.visibility='hidden'};">
            <option value="">Select one</option>
            <option value="Men's top">Men's Top</option>
            <option value="Men's Bottoms">Men's Bottoms</option>
            <option value="Men's Sneakers">Men's Sneakers</option>
            <option value="Lady's Top">Lady's Top</option>
            <option value="Ladys Dress">Lady's Dress</option>
            <option value="Lady's Bottoms">Lady's Bottoms</option>
            <option value="Lady's Shoes">Lady's Shoes</option>
            <option value="Accessory / Other">Accessory(Other)</option>
            </select>

            <br />

            <span>Item Size: </span><select class="reqd" style="visibility:hidden;" type="text" name="box">
            <option value="">Select</option>
            <option value="Small">Small</option>
            <option value="Medium">Medium</option>
            <option value="Large">Large</option>
            <option value="X-Large">X-Large</option>
            <option value="XX-Large">XX-Large</option>
            </select>

            <select class="reqd" style="visibility:hidden;" type="text" name="box2">
            <option value="">Waist</option>
            <option value="28">28</option>
            <option value="29">29</option>
            <option value="30">30</option>
            <option value="31">31</option>
            <option value="32">32</option>
            <option value="33">33</option>
            <option value="34">34</option>
            <option value="35">36</option>
            <option value="36">37</option>
            <option value="37">38</option>
            </select>

            <select class="reqd2" style="visibility:hidden;" type="text" name="box25">
            <option value="">Length</option>
            <option value="28">28</option>
            <option value="29">29</option>
            <option value="30">30</option>
            <option value="31">31</option>
            <option value="32">32</option>
            <option value="33">33</option>
            <option value="34">34</option>
            <option value="36">36</option>
            </select>

            <select class="reqd" style="visibility:hidden;" type="text" name="box3">
            <option value="">Select</option>
            <option value="7">7</option>
            <option value="7.5">7.5</option>
            <option value="8">8</option>
            <option value="8.5">8.5</option>
            <option value="9">9</option>
            <option value="9.5">9.5</option>
            <option value="10">10</option>
            <option value="10.5">10.5</option>
            <option value="11">11</option>
            <option value="11.5">11.5</option>
            <option value="12">12</option>
            <option value="12.5">12.5</option>
            <option value="13">13</option>
            </select>

            <select class="reqd" style="visibility:hidden;" type="text" name="box4">
            <option value="">Select</option>
            <option value="X-Small">X-Small</option>
            <option value="Small">Small</option>
            <option value="Medium">Medium</option>
            <option value="Large">Large</option>
            <option value="X-Large">X-Large</option>
            </select>


            <select class="reqd" style="visibility:hidden;" type="text" name="box5">
            <option value="">Select</option>
            <option value="0">0</option>
            <option value="2">2</option>
            <option value="4">4</option>
            <option value="6">6</option>
            <option value="8">8</option>
            <option value="10">10</option>
            <option value="12">12</option>
            <option value="14">14</option>
            </select>

            <select class="reqd" style="visibility:hidden;" type="text" name="box6">
            <option value="">Select</option>
            <option value="24">24</option>
            <option value="25">25</option>
            <option value="26">26</option>
            <option value="27">27</option>
            <option value="28">28</option>
            <option value="29">29</option>
            <option value="30">30</option>
            <option value="31">31</option>
            <option value="32">32</option>
            </select>

            <select class="reqd" style="visibility:hidden;" type="text" name="box7">
            <option value="">Select</option>
            <option value="5">5</option>
            <option value="5.5">5.5</option>
            <option value="6">6</option>
            <option value="6.5">6.5</option>
            <option value="7">7</option>
            <option value="7.5">7.5</option>
            <option value="8">8</option>
            <option value="8.5">8.5</option>
            <option value="9">9</option>
            <option value="9.5">9.5</option>
            <option value="10">10</option>
            <option value="10.5">10.5</option>
            <option value="11">11</option>
            </select>


            <input class="reqd" style="visibility:hidden;" type="text" name="box8" size="20" value="Specify..."/>

            <br />

            <label>Item Color: </label><input class="reqd" name="itemcolor" type="text" size="16" />

            <br />

            <label>Link (Optional): </label><input name="link" type="text" size="16" />

            <br />
            <br />

            <input type="submit" value="Submit" />

            </form> 

5 个答案:

答案 0 :(得分:1)

试试这个吗?

<script type="text/javascript">
    x=document.getElementsByTagName('form');

    for(i=0;i<x.length;i++)
    {
        document.write(x.item(i).attributes['name'].name);
        document.write(" = ");
        document.write(x.item(i).attributes['name'].value);
        document.write("<br />");
    }

</script>

为我工作^^

答案 1 :(得分:0)

试试这个:

document.write(document.getElementById('listing').getAttribute('name'));

答案 2 :(得分:0)

如果您尝试在表单中按名称访问元素,则只需使用此

即可
<html>
<head>
<script>
alert(f1.in1);  // formName.elementName
</script>
</head>
<body>
<form id='f1' name='f1'>
<input id='in1' name='in1' value='Hello'>
</form>
</body
</html>

答案 3 :(得分:0)

如果您在表单的名称之后,这可行(虽然看起来有点多余)。

document.NordstromListing.name

如果你追求其中一个表单字段的值,你可以这样做:

document.NordstromListing.msrp.value

答案 4 :(得分:0)

试试这个: var my_link = document.forms [&#39; myform&#39;]。elements [&#39; mylink&#39;]。value;