使用 javascript 根据另一个表单的 onChange 更改一个表单值
我似乎无法让这个功能发挥作用。我试图根据“金额”下拉列表中选择的金额更改表单的“业务”输入值。我还缺什么!!!我已经尝试这个一个小时了。
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
删除表单引用将处理您的两个项目。
Removing the form reference would take care of both of your items.
问题是您使用数字作为表单名称。
在名称中添加一些字母即可:
Problem is that you are using a number as form name.
Add some letters to the name and it will work:
HTML:
JavaScript:
现场演示: http://jsfiddle.net/Fx7zh/
对于多种表单,您可以使用以下 JavaScript 代码:
现场演示: http://jsfiddle .net/Fx7zh/2/
HTML:
JavaScript:
Live demo: http://jsfiddle.net/Fx7zh/
For multiple forms, you can use this JavaScript code:
Live demo: http://jsfiddle.net/Fx7zh/2/