[英]Click button in form 1: change value of hidden input in form 2 (pure javascript)
Markup: 标记:
<form action="index.php" method="post" name="formOne">
<input type="button" value="Test" onClick="change('myHiddenField')">
<input type="submit" value="Submit">
</form>
<form action="index.php" method="post" name="formTwo">
<input name="myHiddenField" type="hidden" value="test">
</form>
I want to change the value of the hidden field in formTwo when clicking the button in formOne. 单击formOne中的按钮时,我想更改formTwo中隐藏字段的值。 I want this done by using just simple JavaScript (not JQuery) and without adding IDs to the inputs (just by the name of them.)
我希望通过仅使用简单的JavaScript(而不是JQuery)并且不向输入添加ID(仅按它们的名称)来完成此操作。
I have tried this JavaScript function: 我已经尝试过此JavaScript函数:
function change(field) {
alert(field.value);
}
And if I move the hidden input to formOne it outputs the correct value. 如果将隐藏的输入移动到formOne,它将输出正确的值。 What is the simplest possible way to modify that function to output the value of the hidden field in formTwo?
修改该函数以输出formTwo中隐藏字段的值的最简单方法是什么?
I also tried this, but not working: 我也试过了,但是没有用:
function change(field) {
alert(document.forms[formTwo].field.value);
}
And I would like something that simple to do the task. 我想做些简单的事情。 Is it possible?
可能吗?
<form action="index.php" method="post" name="formOne">
<input type="button" value="Test" onClick="change('myHiddenField')">
<input type="submit" value="Submit">
</form>
<form action="index.php" method="post" name="formTwo">
<input name="myHiddenField" type="hidden" value="test">
</form>
You're missing a closing ")" first of all. 首先,您缺少结尾的“)”。 Secondly you'll need a Javascript to deal with the user's action (this code should be placed before
</html>
tag). 其次,您需要一个Javascript来处理用户的操作(此代码应放在
</html>
标记之前)。
<script>
function change(element)
{
document.getElementsByName(element)[0].value = "other_value";
}
</script>
(This assumes myHiddenField
is the first or only element with this name property.) (这假定
myHiddenField
是具有此name属性的第一个或唯一元素。)
That's all. 就这样。
After testing I found that above answer also only worked if the hidden input was in formOne. 经过测试后,我发现上述答案也仅在隐藏输入在formOne中有效。 Finally I have found a solution though, and I found it here: http://www.w3schools.com/jsref/coll_doc_forms.asp .
最终,我找到了一个解决方案,并且在这里找到了它: http : //www.w3schools.com/jsref/coll_doc_forms.asp 。
function change(index)
{
document.forms['formTwo'].item(index).value = "other_value";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.