Submitting a jQuery ajax form with two submit buttons

前端 未结 8 1020
花落未央
花落未央 2020-12-05 07:31

I have a form that looks like this:

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 08:20

    You can trigger the form submit on the click of the images. This will work with the preventDefault().

    var vote;
    $(".vote_up, .vote_down").click(function(event) {
        vote = $(this).attr("class");
        $(".vote_form").trigger("submit");
    });
    
    $(".vote_form").submit(function(event) { 
        $form = $(this);
        $.post($form.attr("action"), $form.serialize() + "&submit="+ vote, function(data) {
            // do something with response (data)
        });     
        event.preventDefault();
    });
    

提交回复
热议问题