Ajax Form submit with preventDefault

后端 未结 4 827
予麋鹿
予麋鹿 2020-11-30 13:55

I have a normal HTML form in which it is supposed to prevent default form submit and post the values by Ajax. It is not working with my setup Please help me where I went wr

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 14:38

    Wrap your code in DOM Ready

    $(function () {
        var frm = $('#contactForm1');
        frm.submit(function (ev) {
            $.ajax({
                type: frm.attr('method'),
                url: frm.attr('action'),
                data: frm.serialize(),
                success: function (data) {
                    alert('ok');
                }
            });
            ev.preventDefault();
        });
    });
    

提交回复
热议问题