开发者

Unable to get value for checkbox and radio button

I am generating the html using JSON file. Now I need to pass data to the servlet. But the data being passed shows null. I am using jQuery post to pass the data.

My js file has

   $.getJSON('input.json',function(data)
 {
$.each(data,function(j,feild)
        {
            if(this.type=="checkbox")
            {   

            $('body #tabs #tabs-3 #server').append(this.display_name).append(INPUT_CHECKBOX).attr({name:this.name,type:this.type}).append(NEWLINE);
            }       
            else
                if(this.type=="radio")
                {   
                    var radio = '';    
                    var len = feild.values.length; 
                    for (var i = 0; i< len; i++)
                    {        
                        radio +='<input type="radio" name="group2">'+feild.values[i];  
                    }
                    $('body #tabs #tabs-3 #server').append(this.display_name).append(radio).attr({name:this.name,type:this.type}).append(NEWLINE);
                }
        });

The post method contains

function forwarddata()
{
alert('inside the function');
$.post('url', 
      {gender:$('input[type=radio]').val(),
       items:$('input[type=checkbox]').val()!= undefined,
       stores:$('input[type=checkbox]').val()!= undefined
      });
}

Myjson file has

{
"gender":
                   {"display_name":"gender:",
                    "name":"gender",
                    "format":"string",
                    "type":"radio",
                    "format":"string",
                    "values":["male","female"],
                    "isMandatory":"true"
                    },


"items":  
                   { "display_name":"items:",
                     "name":"items",
                     "format":"string",
                     "type":"checkbox",
                     "values":[1,2,3,4],
                     "isMandatory":"true"
                    },


"stores":                       
                   { "display_name":"stores:",
                     "name":"motion",
                     "format":"string",
                     "type":"checkbox",
                     "values":[a,开发者_如何转开发b,c,d],
                     "isMandatory":"true"
                    }

}

When I try printing the data in the servlet it says null... Kindly explain the correct way of posting the data.


It looks to me like you need to format your data into a proper url with parameters --

http://whatever.com/my.php?gender=male& etc.

Depends on what type of script you have receiving the data, but that's the way I like to do it.


$('input[type=checkbox]').val()!= undefined will give you true/false, I hope you are not looking for that. Try this

function forwarddata()
{
alert('inside the function');
$.post('url', 
      {gender:$('input[type=radio]').val(),
       items:$('input[type=checkbox]').val(),
       stores:$('input[type=checkbox]').val()
      });
}


I changed my JSON data.Added name attribute and the below function worked just fine Thanks:)

 function forwardserverdata()
{
alert('inside the function');
$.post(''url, 
      {gender:$('body #tabs #tabs-3 #server input:radio[name=gender]:checked').val(),
       items:$('body #tabs #tabs-3 #server  input:checkbox[name=items]:checked').val()!= undefined,
       stores:$('body #tabs #tabs-3 #server input:checkbox[name=stores]:checked').val()!= undefined
      });

}

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜