简体   繁体   English

在表单1中单击按钮:在表单2中更改隐藏输入的值(纯JavaScript)

[英]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.

相关问题 仅使用纯 javascript 动态生成表单输入文件在单击按钮时递增和递减 - Dynamically generate form input fileds increment and decrement on click of button using pure javascript only 提交表单时更改隐藏的输入值 - Change hidden input value when form is submitted 如何在Vanilla JavaScript中点击按钮获取表单输入值? - How to get form input value on button click in Vanilla JavaScript? javascript更改隐藏值然后提交表单 - javascript to change hidden value then submit form 如何通过纯javascript单击HTML中的单选按钮显示不同的形式 - How to show a different form on click of a radio button in HTML by pure javascript 如果单击按钮,则以表单输入形式输入值(使用Javascript) - Input value in form input if button is clicked (in Javascript) 使用纯 Javascript 将输入表单的值从一个页面显示到另一个页面 - Show value of an input form from a page into another page with pure Javascript 如何使用 JavaScript Pure 检查单选按钮? 使用表单 id=value - How to check a radio button with JavaScript Pure? Use Form id=value 更改输入值 onclick 按钮 - 纯 javascript 或 jQuery - Change input value onclick button - pure javascript or jQuery 尝试在表单中单击“提交”按钮时设置隐藏参数的值 - Trying to set value on hidden parameter on click of submit button in form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM