admin管理员组

文章数量:1396820

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 = "[email protected]";
  if (document.forms['5'].amount.value > 11){
  businessvar = "[email protected]"
  }
  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['formname']" based on a variable populated by something like chPy("formname") etc..... I can't seem to get that to work either.

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 = "[email protected]";
  if (document.forms['5'].amount.value > 11){
  businessvar = "[email protected]"
  }
  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['formname']" based on a variable populated by something like chPy("formname") etc..... I can't seem to get that to work either.

Share Improve this question edited Jul 24, 2012 at 17:13 Oak Bytes 4,7934 gold badges39 silver badges56 bronze badges asked Feb 19, 2011 at 19:45 Andrew SamuelsenAndrew Samuelsen 5,4329 gold badges49 silver badges69 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

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

 <script type="text/javascript">
  function chPy(oSelect)
  {
  var businessvar = "[email protected]";
  if (oSelect.form.amount.value > 11){
  businessvar = "[email protected]"
  }
  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 = "[email protected]";
  if (document.forms['form5'].amount.value > 11){
  businessvar = "[email protected]"
  }
  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.';
}

Live demo: http://jsfiddle/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.';
    }
}

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

本文标签: Changing one form value based on onChange of another with javascriptStack Overflow