html - Updating form values with javascript - Stack Overflow

This works, does anyone see anything I shouldn't be doing?My function that is calledfunction getWe

This works, does anyone see anything I shouldn't be doing?

My function that is called

function getWeight(){
        var weight;
        var quantity = document.dhform.quantity.value;
        var cardSize = document.dhform.cardSize.value;
            weight = quantity * cardSize; 
        document.rates.weight.value = weight;
    }

takes values from these drop down menues

<td><span class="style29">Quantity</span><span class="style1"><br/>
                                <select id="Quantity" name="quantity" size="1">
                                    <option value="250">250</option>
                                    <option value="500">500</option>
                                    <option value="1000" selected>1000</option>
                                    <option value="1500">1500</option>
                                    <option value="2000">2000</option>
                                    <option value="2500">2500</option>
                                    <option value="3000">3000</option>
                                    <option value="4000">4000</option>
                                    <option value="5000">5000</option>
                                </select>
                            </span></td>
<td><p><span class="style1"><span class="style29">Size</span><br/>
                                      <select id="Size" name="Size" size="1" onChange="getWeight()">
                                        <option value="0.00999" selected>8.5 x 3.5</option>
                                        <option value="0.0146">11 x 4</option>
                                      </select>
                            </span></p></td>

Value needs to be inserted into this text box

<td style="width: 115px; height: 49px;"><span class="style16">Weight</span><br/>
                                  <input type="text" id="weight" name="weight" size="10" maxlength="4"/>
                              </td>

This works, does anyone see anything I shouldn't be doing?

My function that is called

function getWeight(){
        var weight;
        var quantity = document.dhform.quantity.value;
        var cardSize = document.dhform.cardSize.value;
            weight = quantity * cardSize; 
        document.rates.weight.value = weight;
    }

takes values from these drop down menues

<td><span class="style29">Quantity</span><span class="style1"><br/>
                                <select id="Quantity" name="quantity" size="1">
                                    <option value="250">250</option>
                                    <option value="500">500</option>
                                    <option value="1000" selected>1000</option>
                                    <option value="1500">1500</option>
                                    <option value="2000">2000</option>
                                    <option value="2500">2500</option>
                                    <option value="3000">3000</option>
                                    <option value="4000">4000</option>
                                    <option value="5000">5000</option>
                                </select>
                            </span></td>
<td><p><span class="style1"><span class="style29">Size</span><br/>
                                      <select id="Size" name="Size" size="1" onChange="getWeight()">
                                        <option value="0.00999" selected>8.5 x 3.5</option>
                                        <option value="0.0146">11 x 4</option>
                                      </select>
                            </span></p></td>

Value needs to be inserted into this text box

<td style="width: 115px; height: 49px;"><span class="style16">Weight</span><br/>
                                  <input type="text" id="weight" name="weight" size="10" maxlength="4"/>
                              </td>
Share Improve this question edited Feb 10, 2014 at 16:11 BenMorel 36.8k52 gold badges206 silver badges337 bronze badges asked Apr 30, 2010 at 14:55 shinjuoshinjuo 21.1k24 gold badges76 silver badges104 bronze badges 4
  • This is too general and sounds like "How do I use javascript?". Please be more specific. – D'Arcy Rittich Commented Apr 30, 2010 at 14:58
  • you need ajax i think, you can google about it or be more specific to your question with sample code you are using. – Sarfraz Commented Apr 30, 2010 at 14:59
  • Ajax may well not be necessary; where Ajax es in is if the determination of the updated value requires going back to the server to fetch data. In many cases it doesn't. – Jacob Mattison Commented Apr 30, 2010 at 15:01
  • Not easy to explain myself anymore than I did when I do not even know what I am lookng for. I am not looking for an answer just a point in a direction – shinjuo Commented Apr 30, 2010 at 15:10
Add a ment  | 

1 Answer 1

Reset to default 2

Yes, it's done with Javascript. Let's say the "choose something" part is a drop-down (an HTML select box). You add an "onchange" event handler to that select box which fires a javascript function (which will automatically get the changed select box element as a parameter). Within that function, you use the value of the select box to determine what you want the value of the other box to be, and you update that other box's value.

Example:

<head>
<script language="JavaScript">
    function setToy(dropDown) {
        var pet = dropDown.options[dropDown.selectedIndex].value);
        var newBox = document.getElementById("toy");
        var toyText = "";
        switch(pet) {
           case "dog": toyText = "bone";
           case "cat": toyText = "mouse";
           default:toyText = "";
        }
        newBox.innerHTML = toyText;
    } 
</script></head>
<body>
<select name="petDropDown" onChange="updateToy(this)">
    <option value="dog">Dog</option>
    <option value="cat">Cat</option>
</select><br />
Preferred Toy: <input id="toy" />
</body>

I'll add that if you do this stuff a lot, you should look into jQuery, which makes this kind of thing much easier.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745454643a4628428.html

相关推荐

  • html - Updating form values with javascript - Stack Overflow

    This works, does anyone see anything I shouldn't be doing?My function that is calledfunction getWe

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信