Pass a hidden field value based on which button is clicked with JavaScript

前端 未结 4 1751
忘掉有多难
忘掉有多难 2021-02-20 01:43

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

4条回答
  •  伪装坚强ぢ
    2021-02-20 02:43

    1. Forgive me if the answer to this seems obvious, but why use a hidden field at all? already works; browsers will send the value of the submit button that was clicked, along with the rest of the form data.

    2. The element works perfectly in Chrome/Safari/Firefox, but fails in IE7 (as Lèse majesté pointed out in his comment above); if you don't care about IE, that'd be the way to go.

    3. You could also use an element, just remember that the browser submits it as two fields: name.x and name.y. Also, you can't set its value, but that shouldn't be a problem if you're only interested in the presence of the field.

提交回复
热议问题