HTML5 required attribute not working

前端 未结 2 952
余生分开走
余生分开走 2021-01-29 11:27

I want to create a email contact form without refreshing the page. So i added jquery im my html file. I used html required attribute ti check if the fields are empty. But when i

相关标签:
2条回答
  • 2021-01-29 11:53

    Form validation is only triggered on the submit event of the form unless you trigger it elsewhere. Do the AJAX there instead, like this:

    $(document).ready(function(){
        $('#mycontactform').submit(function(e){
            e.preventDefault();
            $.post("send.php", $(this).serialize(), function(response) {   
                $('#success').html(response);
                //$('#success').hide('slow');
            });
        });
    });
    
    0 讨论(0)
  • 2021-01-29 12:18

    You are sending the AJAX request when the user clicks on the button, not when they submit the form. Change .click() to .on('submit') and attach it to the form, not the button:

    $('#mycontactform').on( 'submit', function () {
        ...
    

    Demo: http://jsfiddle.net/QjPka/

    0 讨论(0)
提交回复
热议问题