在提交时设置表单隐藏值 [英] Set form hidden value on submit

查看:117
本文介绍了在提交时设置表单隐藏值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,我想通过javascript发送隐藏的输入,该怎么做? F.ex:

I got a form, and I want to send hidden input by javascript, how to do it? F.ex:

<input id="total" type="hidden" value="" name="total" />

当有人单击提交时,我想为其设置值

And when someone click submit I want to set it value

推荐答案

您是否要立即更改隐藏输入的值?当然,可以通过onsubmit事件来实现类似的目的.

Are you trying to instantaneously change the value of the hidden input? Something like that can be achieved with the onsubmit event, of course.

var submit = document.getElementById("submit");
var hidden = document.getElementById("hidden");

var eventListener = function(el, type, listener) {
    if(el.addEventListener) {
        el.addEventListener(type, listener, false);
    } else if(el.attatchEvent) {
        el.attatchEvent("on"+type, listener);
    } else {
        el['on'+type] = listener;
    }
};

var onsubmit = function() {
    var newVal = "value to set for hidden input";

    hidden.value = newVal;
};

eventListener(submit, "submit", onsubmit);

上面的代码将侦听Submit按钮上的Submit事件,并在触发时将更改隐藏输入的值.为了进一步详细回答任何问题,我们需要一个更具描述性的问题.否则,我真的不知道你想要什么.

The above code will listen to the submit event on the submit button and when triggered, it will change the value of the hidden input. To go into further detail with any answer, we'd need a more descriptive question. Otherwise, I really don't know what you want.

这篇关于在提交时设置表单隐藏值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆