jeudi 5 février 2015

Append checkboxes vlaue in Jquery

Basic idea is to capture around 20 checkboxes values in one single form field. I tried the following code to append the values. Here I am showing with just 2 fields. But I couldnt get it. If the checkbox is checked, value should append or else it should not proceed to append. All appended values need to be in hypen(-) separated.


Jquery library used : 1.7.2


HTML:



<input name=""val1"" class=""other"" id=""field1"" type=""checkbox"" value=""value1"">value1
<input name=""val2"" class=""other"" id=""field2"" type=""checkbox"" value=""value2"">value2

<div id=""test""></div>


Jquery:



//<![CDATA[

$("#field1, #field2").click(function () {

if ($(this).is(':checked')) {

var name = $(this).val();
var combine;
alert('i am in ');

//var name=$("#field"+i).val();

combine += name + '-';
alert('inside');
alert(combine);
//i++;

} else {
alert('else');
$(this).attr('value', '');

}

$('#test').html('<input size="100" type="text" name="product" value="' + combine + '"/>');
alert('end');
}); //]]>


JS fiddle





Aucun commentaire:

Enregistrer un commentaire