根据使用JavaScript单击的按钮传递隐藏的字段值 [英] Pass a hidden field value based on which button is clicked with JavaScript

查看:66
本文介绍了根据使用JavaScript单击的按钮传递隐藏的字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个按钮的表单,每个按钮都有onclick = this.form.submit()。我在表单中有一个隐藏字段,我希望字段的值根据单击的按钮而有所不同。最好的方法是什么?

I have a form with two buttons, each with onclick = this.form.submit(). I have a hidden field in the form, and I would like the value of the field to be different based on which button is clicked. What would be the best way to do this?

此外,在有人说出答案之前,jQuery在这种情况下不是一个选项。

Also, before anyone says it the answers, jQuery is not an option in this case.

推荐答案

使用原型: - )

但严肃地说:


  1. id 添加到隐藏字段

  2. 之前你在每个处理程序中提交表单:

  1. Add an id to the hidden field
  2. Before you submit the form in each handler:

document.getElementById(hiddenId)。value =mySpecialValue;

//或(取决于您所在的onclick处理程序)

document.getElementById(hiddenId)。value =myOtherSpecialValue;

以与现在相同的方式提交表格。

Submit the form the same way you are now.

推荐前例:

< input id =buttonAtype =buttonvalue =do somethingonclick =buttonA_clickHandler(event);/>

...

function buttonA_clickHandler(event) {
    document.getElementById('hiddenId').value = whatever;
    document.getElementById('theForm').submit();
}

重复另一个按钮。

这篇关于根据使用JavaScript单击的按钮传递隐藏的字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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