微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐