开发者

Changing one form value based on onChange of another with javascript

I can't seem to get this function to work out. I'm trying to change the "business" input value of my form based on an amount selected in the "amount" dropdown. What am I missing!!! I've been trying this for an hour.

  <script type="text/javascript">
  function chPy()
  {
  var businessvar = "paypals@ramatico.com";
  if (document.forms['5'].amount.value > 11){
  businessvar = "paypall@ramatico.com"
  }
  document.forms['5'].business.value = businessvar;
  }
  </script>

  <form name="5">
  <select name="amount" OnChange="chPy()">
      <option value="na">Select</option>
      <option value="1.00">$1</option>
      <option value="5.00">$5</option>
      <option value="10.00">$10</option>
      <option value="15.00">$15</option>
      <option value="20.00">$20</option>
      <option value="25.00">$25</option>
  </select> 
  <input name="business" type="text" value="">
  </form>

Ok. The next step. I have about 100 of these forms on a page. Instead of creating one for each form, I'd like to have one script that changes based on which form is being changed with "onChange". Each of the forms on the page have values of the same name (but not ID) (they are for paypal). I think it will be ok if I can change the"formname" in the following: "document.forms['开发者_Go百科formname']" based on a variable populated by something like chPy("formname") etc..... I can't seem to get that to work either.


Removing the form reference would take care of both of your items.

 <script type="text/javascript">
  function chPy(oSelect)
  {
  var businessvar = "paypals@ramatico.com";
  if (oSelect.form.amount.value > 11){
  businessvar = "paypall@ramatico.com"
  }
  oSelect.form.business.value = businessvar;
  }
  </script>

  <form name="5">
  <select name="amount" OnChange="chPy(this)">
      <option value="na">Select</option>
      <option value="1.00">$1</option>
      <option value="5.00">$5</option>
      <option value="10.00">$10</option>
      <option value="15.00">$15</option>
      <option value="20.00">$20</option>
      <option value="25.00">$25</option>
  </select> 
  <input name="business" type="text" value="">
  </form>


Problem is that you are using a number as form name.

Add some letters to the name and it will work:

<script type="text/javascript">
  function chPy()
  {
  var businessvar = "paypals@XYZ.com";
  if (document.forms['form5'].amount.value > 11){
  businessvar = "paypall@XYZ.com"
  }
  document.forms['form5'].business.value = businessvar;
  }
  </script>

  <form name="form5">
  <select name="amount" OnChange="chPy()">
      <option value="na">Select</option>
      <option value="1.00">$1</option>
      <option value="5.00">$5</option>
      <option value="10.00">$10</option>
      <option value="15.00">$15</option>
      <option value="20.00">$20</option>
      <option value="25.00">$25</option>
  </select> 
  <input name="business" type="text" value="">
  </form>


HTML:

<form name="form5">
    <select name="amount">
        <option value="na">Select</option>
        <option value="1.00">$1</option>
        <option value="5.00">$5</option>
        <option value="10.00">$10</option>
        <option value="15.00">$15</option>
        <option value="20.00">$20</option>
        <option value="25.00">$25</option>
    </select> 
    <input type="text" name="business">
</form>

JavaScript:

var form5 = document.forms['form5'];

form5.amount.onchange = function() {
    form5.business.value =
        (this.value > 11 ? 'paypall' : 'paypals') + '@ramatico.com';
}

Live demo: http://jsfiddle.net/Fx7zh/


For multiple forms, you can use this JavaScript code:

for (var i = 0, l = document.forms.length; i < l; i++) {
    document.forms[i].amount.onchange = function(i) {
        this.form.business.value =
            (this.value > 11 ? 'paypall' : 'paypals') + '@ramatico.com';
    }
}

Live demo: http://jsfiddle.net/Fx7zh/2/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜