0

我正在尝试通过 Ajax/Jquery 提交表单并在手风琴步骤过程中从 Servlet 获得响应,但是对于我的一生,我无法弄清楚这一点。我有自己的表格,我有自己的手风琴,但是当我把这两者放在一起时,它就不起作用了。

这是我把它放在一起的代码:

$(document).ready(function(){

$('label.form-field-label-required').append('&nbsp;<strong>*</strong>');

// accordion functions
var accordion = $("#accordion").accordion({
      event: false,
      autoheight: true,
      animated: "slide",
});
var current = 0; 

$.validator.addMethod("pageRequired", function(value, element) {
    var $element = $(element)
    function match(index) {
        return current == index && $(element).parents("#accordion").length;
    }
    if (match(0) || match(1) || match(2)) {
        return !this.optional(element);
    }
    return "dependency-mismatch";
}, $.validator.messages.required)

var v = $("#cmaForm").validate({
    submitHandler: function() { 
         $.ajax({
                    type:"post",
                    url:"ActionServlet",
                    data:"name="+name+"&sex="+gender,
                    success: function(response){

                    // Change only the header of the file.
                    $("h1").text(response);
                    },
                }); 
                            if (v2.form()) {
                        accordion.accordion("activate", 1);
                        current = 1;
                    }
                }
        });

表格代码:

    <h4><a href="#">Employee Personal Information</a></h4>
    <div>
            <form name="cmaForm" id="cmaForm" action="" method="post">
<h1 style="text-align:center">Hello Guest</h1>
      <p>Please fill out your name and gender: <br></br>
  Name: <input type="text" name="name" id="name"/><br/>
  <input type="radio" name="sex" value="male"/> Male.<br/>
  <input type="radio" name="sex" value="female"/> Female. </p>
      <p>
         <input name="formNext1" type="submit" class="open1 nextbutton" value="Next" alt="Next" title="Next" />
      </p>
    </form>

应该发生的是,成功后,H1 文本应更改以反映输入数据,然后第一个手风琴关闭,第二个手风琴打开。到目前为止,所发生的只是页面刷新,然后它会显示在地址栏中:

http://localhost/test/index.htm?name=bnhjhj&sex=female#

但没有其他事情发生。

我只包含了第一个手风琴代码来限制屏幕上的代码数量。如果你需要其余的,我可以发布它。

提前致谢。

服务代码

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ActionServlet extends HttpServlet implements Servlet
{

    private static final long serialVersionUID = 1L;

    /**
     * 
     */
    public ActionServlet()
    {
        super();
    }

    /**
     * 
     */
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    {
        doPost(req, res);
    }

       public void doPost (HttpServletRequest request, HttpServletResponse response) 
               throws ServletException, IOException {
                    String name= request.getParameter("name");
                    String gender= request.getParameter("sex");
                    PrintWriter out= response.getWriter();
                        if(gender.equals("male")){
                            out.print("Hello Mr "+ name +" your request was sent successfully.");
                            }
                            else{
                            out.print("Hello Miss "+ name +" your request was sent successfully.");
                        }
                }
}
4

1 回答 1

0

在调用 jQuery ajax() 的地方,将 data 属性更改为 JSON 而不是字符串。例子

$.ajax({ ..., data: { "name": name, "gender": gender }});
于 2012-09-13T20:13:28.457 回答