jquery - Serialize those fields which are filled or selected -
i want serialize fields filled or selected included (textboxes, checkboxes , selectmenu).
html section
<div id="all_field_0"> <input type="text" name="a_0"/> <input type="text" name="b_0"/> <select name="c_0"> <option value="1">a</option> <option value="2">b</option> <option value="3">c</option> </select> <select name="d_0"> <option value="1">a</option> <option value="2">b</option> <option value="3">c</option> </select> </div> <div id="all_field_1"> <input type="text" name="a_1"/> <input type="text" name="b_1"/> <select name="c_1"> <option value="1">a</option> <option value="2">b</option> <option value="3">c</option> </select> <select name="d_1"> <option value="1">a</option> <option value="2">b</option> <option value="3">c</option> </select> </div> jquery section
this code serialize fields
var field_div = ""; for(var fc=0; fc<2; fc++) { field_div = "#all_field_"+fc; $(field_div + " :input,select").serializeobject() }
the below script seems satisfy looking for. each field needs bit of logic see if values have been entered. input , select work selected them check box needs 1 more piece of logic.
var field_div = ""; for(var fc=0; fc<2; fc++) { field_div = "#all_field_"+fc; //check see if input has value if($(field_div + " :input,select").val()) { //field has $(field_div + " :input,select").serializeobject(); //serialize , store somewhere } else { //field doesn't have } //check selected checkbox if($(field_div + " :checkbox:checked").length > 0) { //checkbox checked $(field_div + " :checkbox:").serializeobject(); //serialize , store somewhere } else { //checkbox not checked } }
Comments
Post a Comment