Submitting a jQuery ajax form with two submit buttons

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

I have a form that looks like this:

相关标签:
8条回答
  • 2020-12-05 08:21

    I don't get how return false and preventDefault failed to do their job. Maybe try replacing the image buttons with linked images:

    <a href="#" class="vote_down"><img src="vote_down.png"/></a>
    
    $('#vote_form > a').click(function(e) {
        e.preventDefault();
    
        //one way to know which image was clicked
        alert($(this).attr('class'));
    
        $.post(...
    });
    

    You can always ensure that a form does not submit by binding to the submit event, e.g.:

    $('#vote_form').submit(function() {
        return false;
    });
    
    0 讨论(0)
  • 2020-12-05 08:22

    Try adding onsubmit="return false;" to your form, and then submitting your form with javascript:

    <form action="/vote/" method="post" name="vote_form" class="vote_form" onsubmit="return false;">
        <input type="hidden" name="question_id" value="10" />
        <input type="image" src="vote_down.png" class="vote_down" name="submit" value="down" onclick="imageClicked(this)"/>
        <input type="image" src="vote_up.png" class="vote_up" name="submit" value="up" onclick="imageClicked(this)"/>
    </form>
    
    <script>
    function imageClicked(img) {
        alert(img.className);
        document.forms["vote_form"].submit();
    }
    </script>
    
    0 讨论(0)
提交回复
热议问题