如何使用ajax和modelAttribute仅使用复选框提交表单

时间:2015-03-21 23:33:33

标签: javascript jquery ajax forms jsp

我的表单只有复选框。我使用userForm.serialize()来获取表单数据,但是,警告打印的内容类似于" male = true& female = on& status = true"当男性和身份被检查。 ajax调用因" IN ERROR"失败,未调用服务器操作。如何使用ajax提交此表单,以便model属性从JSP获取数据? 非常感谢。

控制器:

 @RequestMapping(value = "/userSelectionSubmit",  method = { RequestMethod.POST, RequestMethod.GET })
   public ModelAndView userSubmit(@ModelAttribute UserSelections usersel)
   {
       boolean bMale = usersel.isMale();
        boolean bFemale = usersel.isFemale ();
       boolean bSingle = usersel.isSinlge();
   }

JSP(示例):

  <form:form id= "userForm" name="userForm"  method="post" 
     modelAttribute="userSelections" action="UserSelectSubmit.htm">
      <form:checkbox path="male" id="male" name="male" />male
     <form:checkbox path="female" id="female" name="female" />female
    <form:checkbox path="Single" id="Single" name="Single" />Single
    <input id='btnContinue' type="button" value="Continue"  />

    <Script>
        $(document).ready(function()
        {                                                           
            $("#btnContinue").click(function()
            {
               alert($("#userForm").serialize()); // this printed male=true&female=on&status=true   

               $.ajax({
               type: 'POST',
               url: "UserSelectSubmit.htm",
               contentType: "application/json",
              data: $("#userForm").serialize(), 
               success: function(data) 
               {
                        .....
                },
                 error: function(e) 
                 {
                       alert('IN ERROR ');
                     }
             });
          })
        })

0 个答案:

没有答案