使用 javascript 根据另一个表单的 onChange 更改一个表单值

发布于 2024-10-18 03:09:23 字数 1377 浏览 1 评论 0原文

我似乎无法让这个功能发挥作用。我试图根据“金额”下拉列表中选择的金额更改表单的“业务”输入值。我还缺什么!!!我已经尝试这个一个小时了。

  <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>

好的。下一步。我在一页上有大约 100 个这样的表格。我不想为每种表单创建一个脚本,而是希望有一个脚本可以根据使用“onChange”更改的表单进行更改。页面上的每个表单都有相同名称的值(但不是 ID)(它们用于 paypal)。我认为如果我可以更改以下中的“formname”就可以了:“document.forms['formname']”基于由 chPy(“formname”) 等填充的变量......我可以似乎也无法让它发挥作用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぽ尐不点ル 2024-10-25 03:09:23

删除表单引用将处理您的两个项目。

 <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>

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>
段念尘 2024-10-25 03:09:23

问题是您使用数字作为表单名称。

在名称中添加一些字母即可:

<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>

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>
那伤。 2024-10-25 03:09:23

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';
}

现场演示: http://jsfiddle.net/Fx7zh/


对于多种表单,您可以使用以下 JavaScript 代码:

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';
    }
}

现场演示: http://jsfiddle .net/Fx7zh/2/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文