Pass extra variable when submitting a form using jQuery

前端 未结 5 2150
野性不改
野性不改 2021-02-12 12:53

I have a form in which the Submit button does not sit within the

tags. The submit button has an click handler to submit the form via jQuer
5条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-12 13:32

    This seems to be an old question, but since I just ran into a similar issue and found a good solution (for me), I thought I'd post it:

     $('#admin_form').trigger('submit', [ { 'variable_name': true } ]);
    

    or

    $('#admin_form').trigger('submit', [ 'variable_name', 'true' ]);
    

    depending on whether you want to pass a variable called variable_name with value true or you want to pass 2 separate variables.

    And just to be clear, this will not of course automatically submit variable_name with the rest of the form fields, but if you have a submit handler, as I do in jQuery, the variables will be passed as arguments, after the event.

提交回复
热议问题