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

前端 未结 4 1719
忘掉有多难
忘掉有多难 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:25

    use Prototype :-)

    But in all seriousness:

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

      document.getElementById("hiddenId").value = "mySpecialValue";

      //or (depending on which onclick handler you are in)

      document.getElementById("hiddenId").value = "myOtherSpecialValue";

    3. Submit the form the same way you are now.

    Recommended ex:

    ...

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

    repeat for the other button.

提交回复
热议问题