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

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

    Assuming you've got your hidden field setup something like so:

    
    

    You could just set its value in a common onclick handler for your buttons:

    function buttonClick(theButton){
        document.getElementById('clicked_button').value = theButton.name;
        return true;
    }
    

    And then your submit buttons would be:

    
    
    

提交回复
热议问题