javascript-如何使用jquery或ajax单击提交表单

在我的代码中,我有一个表格

 <form id="form_enquiry" name="enquiry-form">
        <ul>
            <li>
                <label>Name</label>
                <input name="name" type="text" id="name" class="required"/>
            </li>
            <li>
                <label>Email</label>
                <input name="email" type="text" id="email" class="required"/>
            </li>
            <li>
                <label>Telephone</label>
                <input name="telephone" type="text" id="telephone" class="required"/>
            </li>
            <li>
                <label>Message</label>
                <textarea rows="4" name="message" id="message" class="required"></textarea>
            </li>
             <li>
                <label>How did you hear about us?</label>
                <div class="select-arrow">
                <select name="select_option" id="select_option" class="required">
                    <option value="" selected="selected">— — —</option>
                    <option value="I am a repeat customer">I am a repeat customer</option>
                    <option value="You where recommended to me">You where recommended to me</option>
                    <option value="Google Search">Google Search</option>
                    <option value="Travelzoo">Travelzoo</option>
                    <option value="Newspaper article">Newspaper article</option>
                    <option value="Facebook">Facebook</option>
                </select>
                </div>
            </li>
             <li>
                <button type="submit" id="enquiry_submit">Send</button>
            </li>
          </ul>
        </form>

我使用jQuery代码提交的这种形式

$("#form_enquiry").submit(function (e) {
        e.preventDefault(); // this will prevent from submitting the form.
          //alert('check');
            $("#form_enquiry").validate({
             submitHandler: function(form) {

                var name1 = $('#name').val();
                var email1 = $('#email').val();
                var telephone1 = $('#telephone').val();
                var message1 = $('#message').val();
                var select_option1 = $('#select_option').val();
                var site_url = <?= $_SERVER['REQUEST_URI'] ?>;
                    if(name1 && email1 && telephone1 && message1 && select_option1) 
                    {
                        $.ajax({
                            type: 'post',
                            url: '<?= base_url()?>'+'frontend/form_enquiry',
                            data: 'name='+name1+'&email='+email1+'&telephone='+telephone1+'&message='+message1+'&select_option='+select_option1+'&site_url='+site_url
                            //success: function(data) { $('#login_err').html('Success ...'); }
                        });
                    }
                }

            });
        });

其工作正常,但需要两次单击发送按钮.
我想单击提交表单,代码中有什么错误帮助我…

解决方法:

如果您使用的是jQuery validate,则不必手动绑定Submit事件,因为它可以为您完成.只需单独使用:

$("#form_enquiry").validate({
    submitHandler: function (form) {

        var name1 = $('#name').val();
        var email1 = $('#email').val();
        var telephone1 = $('#telephone').val();
        var message1 = $('#message').val();
        var select_option1 = $('#select_option').val();
        var site_url = <?= $_SERVER['REQUEST_URI'] ?> ;
        if (name1 && email1 && telephone1 && message1 && select_option1) {
            $.ajax({
                type: 'post',
                url: '<?= base_url()?>' + 'frontend/form_enquiry',
                data: 'name=' + name1 + '&email=' + email1 + '&telephone=' + telephone1 + '&message=' + message1 + '&select_option=' + select_option1 + '&site_url=' + site_url
                //success: function(data) { $('#login_err').html('Success ...'); }
            });
        }
    }

});

演示:http://jsfiddle.net/54hcvcbk/

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...