简体   繁体   English

jQuery使用method = post提交表单

[英]Submitting form by jquery using method=post

i want to submit a form by jquery. 我想通过jQuery提交表格。 i write the query form as follow 我写查询表格如下

$("#form").submit(function(event) {
    if(!$("#change").hasClass("has-error") && !$("#name1").hasClass("has-error") 
            && !$("#contact1").hasClass("has-error") && !$("#batch1").hasClass("has-error")){
            alert("submitting");
            event.preventDefault();
            var $form=$(this);
            name1=$("#name").val();
            contact1=$("#contact").val();
            email1=$("#email").val();
            city1=$form.find("input[name='city']").val();
            company1=$form.find("input[name='company']").val();
            url=$form.attr("action");
            var posting= $.post("some_link.php",{ name:name1,contact:contact1,email:email1,city:city1,company:company1});
            posting.done(function(data){
                alert("form submitted successfully");
                )
            })
            $("#reset").click();
            return false;

            }
    else return false;

});

but this is posting form by GET method not by POST method and also url form is redirected to other php page instead for remaining on this page. 但这是通过GET方法而不是POST方法发布的表单,并且url表单也被重定向到其他php页面,而不是保留在该页面上。

here is how link look like http://www.example.com/current_page.php?name=jhgjk&contact=lksjf&email=lkdj%40ldkjf.clj&city=&company= but i want a normal post method as usually done by including method="post" in form 这是链接的外观,看起来像http://www.example.com/current_page.php?name=jhgjk&contact=lksjf&email=lkdj%40ldkjf.clj&city=&company=但我想要一个普通的发布方法,通常通过包括method="post"形式

EDIT i got the mistake i was doing. 编辑我弄错了我在做。 i was missing a closing bracket for $.post and this was the reason the code was not working and i was banging my head. 我错过了$ .post的右括号,这就是代码无法正常工作的原因,而且我还在努力。

I think you'd be much better using a technology called $.ajax : 我认为使用$ .ajax技术会更好:

$("#form").submit(function(e) {
    if(!$("#change").hasClass("has-error") && !$("#name1").hasClass("has-error") 
            && !$("#contact1").hasClass("has-error") && !$("#batch1").hasClass("has-error")){


            e.preventDefault();
            alert("submitting");

            $.ajax({ 
               url: 'some-link.php',
               type: 'POST',
               data: $(this).serialize(),
               success: function(data) {
                  alert('success!');
               },
               error: function(data) {
                  alert('error');
               }
            });


            }
    else return false;

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM