在外部提交一个带有INPUT的表单,并在修改值之后提交 [英] Submitting a form with an INPUT outside it, and after modiying a value
问题描述
我有一个表单
,里面有几个< input>
标签。我有一个提交按钮(实际上是< input>
类型为 BUTTON
,不 SUBMIT
),表示 。我的表单设置类似于这个 -
I have a form
, with a couple of <input>
tags inside. I have a submit button (actually an <input>
of type BUTTON
, not SUBMIT
) that is outside the form. I have the form set up similar to this —
<form name="testform" id="testform" action="test.jsp" onsubmit="return modify_value();" method="POST">
<input name="test1" id="test1" type="TEXT" value="A"/>
<input name="test2" id="test2" type="TEXT" value="B"/>
<input name="test3" id="test3" type="HIDDEN"/>
</form>
表单外的提交按钮是以这种方式定义的 -
The submit button, which is outside the form, is defined this way —
<input type="BUTTON" id="_submit" onclick="document.forms[0].submit();"/>
和 modify_value()
JavaScript方法看起来像这样 -
And the modify_value()
JavaScript method looks like this —
function modify_value()
{
var hidden_field = document.getElementById('test3');
hidden_field.value = 'new_test_value_set_on_clicking';
return true;
}
当点击提交按钮时,我正在尝试修改表单提交之前的 test3
元素。由于某种原因,我无法读取我的servlet中的新值。
When the submit button is clicked, I am trying to modify the value of the test3
element before the form gets submitted. For some reason, I can never read the new value in my servlet.
替代方法 - (不起作用) WORKS!
Alternate Method - (Doesn't Work Either) WORKS!
我已经尝试以稍微不同的方式提交表单 - 通过设置按钮的 onclick
event指向 modify_value()
方法,并在该方法的最后一行,调用 form.submit()
而不是返回值(编辑:当然,删除表单中的 onsubmit
属性)。
I have tried submitting the form in a slightly different way as well - by setting the button's onclick
event to point to the modify_value()
method and in the last line of that method, calling form.submit()
instead of returning a value ( And of course, removing the onsubmit
attribute in the form). This doesn't work either.
我在这里做错什么?
推荐答案
你可以尝试这个
<input type="BUTTON" id="_submit" onclick="modify_value()"/>
function modify_value()
{
var hidden_field = document.getElementById('test3');
hidden_field.value = 'new_test_value_set_on_clicking';
document.getElementById("testform").Submit();
}
这篇关于在外部提交一个带有INPUT的表单,并在修改值之后提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!