简体   繁体   English

如何在使用javascript提交之前更改隐藏的输入值?

[英]How to change a hidden input value just before submitting using javascript?

I am a very beginner in javascript and I would like to change a single input value when the user selects an option with a form, thhen submit it to the same page. 我是javascript的初学者,当用户选择带有表单的选项时,我想更改单个输入值,然后将其提交到同一页面。

here is the Html code: 这是Html代码:

<form id="form" action="thispage.jsp" method="post">
<input id="action_type" type="hidden" name="action_type" value="firstAction" />
<table class="choose">
    <tr>
        <td>Choose your test</td>
        <td><select id="select_test" name="test_name">
            <option value="test0"></option>
            <option value="test1"></option>
        </select></td>
    </tr>
</table>

And the javascript function: 和javascript函数:

form = document.getElementById("form");
action = document.getElementById("action_type");
select = document.getElementById("select_test");
select.onchange = function() {
    action.setAttribute("value", "otherAction");
    form.submit();
}

And this returns an Exception, what is wrong with this code ? 这会返回一个Exception,这段代码出了什么问题?

I think the form is not defined. 我认为表格没有定义。 try to add a var before the form. 尝试在表单之前添加var。

It doesn't seem to be a problem with JavaScript, but with your JSP code. 它似乎不是JavaScript的问题,而是您的JSP代码。 Try debugging the code and check where it's failing. 尝试调试代码并检查它失败的位置。

I think nullPointer shows because there is no selected option. 我认为nullPointer显示是因为没有选中的选项。 Since the code does not have any innerHTML on it, you cant choose anything. 由于代码上没有任何innerHTML ,因此您无法选择任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM