jQuery ajax提交form表单

这篇博文主要给大家带来使用jquery提交form表单,其实很简单那,我就直接上代码了:
html表单代码:

 <body>
    <form action="" id="myform">
        用户名<input type="text" name="username"/>
        密码<input type="password" name="password"/>
        性别<input type="radio" name="sex" value="男人">man
           <input type="radio" name="sex" value="女人">woman
    </form>

    <a href="#" style="text-decoration: none;">使用ajax提交表单数据</a>
  </body>

使用jQuery ajax提交表单代码:

<script type="text/javascript"> $(document).ready(function(){ $("a").click(function(){ $.ajax({ type:'post', url:'FormServlet', data:$("#myform").serialize(), cache:false, dataType:'json', success:function(data){ alert("请求成功"); } }); }); }); </script>

FormSerlvet:

request.setCharacterEncoding("UTF-8");
String userName = request.getParameter("username");
String passWord = request.getParameter("password");
String sex = request.getParameter("sex");   System.out.println("name:"+userName+"==pass:"+passWord+"==sex:"+sex);

源码下载

你可能感兴趣的:(Ajax,表单,提交)