开发者

Passing selected check boxes using jQuery servlet [duplicate]

This question already has a开发者_运维问答nswers here: How to send request parameter array to servlet using jQuery $.ajax? (4 answers) Closed 6 years ago.

How can I pass selected checkbox values to another servlet (not below one) using jQuery post method?

This is my servlet code for generationg check boxes on the html page I am using json

for(int i=0;i<roles.size();i++){
                 JSONObject msg = new JSONObject();
                msg.put("selector", "#roles");
            //<input type=radio name="radio" value="<%=bean.getSno()%>"></td>
    msg.put("msg", 
            "<input type=checkbox id="+'"'+"checkedroles"+'"'+"name="+'"'+"checkedroles"+'"'
            +"value="+roles.get(i)+">"+roles.get(i)+"<br>");
    messages.put(msg);

            }

this is the jquercode

* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function(){
  //global vars
  var UserName = $("#UserName"); //user name field
  var FirstName = $("#FirstName"); //first name
  var LastName=$("#LastName");
  var StreetAdress=$("#StreetAdress");
  var City=$("#City");
  var Province=$("#Province");
  var Organization=$("#Organization");
  var email=$("#email");
  var phone=$("phone");
  var checkedroles=$("#checkedroles");
  var selected = new Array();

    function checkCommentsForm(){
   return true;
  }
  $("input:checkbox[name=checkedroles]:checked").each(function() {
       selected.push($(this).val());
  });


  //When form submitted
  $("#Reg").submit(function(){
    if(checkCommentsForm()){
      $.ajax({
        type: "post",
        url: "loginProcess.jsp",
        data: {user : UserName.val(), fname : FirstName.val(),lname : LastName.val()
    ,stAddress:StreetAdress.val(),city:City.val(),prov:Province.val(),org:Organization.val()
    ,mail:email.val(),ph:phone.val(),'ch[]':selected},
        success: function(data) {
                                    }

});
        }

  });
});

also pls tell me how to retrieve the array in the servlet


You haven't shown your HTML but from the looks of it, it seems like these values are in a form.

You could then use ('#Reg').serialize() instead. This will Encode a set of form elements as a string for submission. - it will include all successful controls so all checked checkboxes will be included automatically as will all enabled text fields.

$("#Reg").submit(function(){
    if(checkCommentsForm()){
      $.ajax({
            type: "post",
            url: "loginProcess.jsp",
            data: $(this).serialize(),
            success: function(data) {
                    }
        });
    }
});

To retrieve the submitted values in your servlet, use the ServletRequest#getParameter() and ServletRequest#getParameterValues() methods. The former for when you have just one value for a parameter (for example, a test field) and the latter when you can have multiple values with that parameter name, for example, multiple checkboxes with the same name as is the case in your question. So you'd write something like this in your servlet:

String [] checked = request.getParameterValues("checkboxname");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜