I have a couple checkboxes, on clicking each I would like their value to go into a hidden field with jQuery seperated by commas, if they are all unchecked, the hidden field value would go back to -1. Below is what I have tried.
<input type="hidden" name="agreement_ids" value="-1">
<input type="checkbox" id="agreementChecker-1" name="agreeCheck-1" value="35" class="mandatory agreeChecker">
<input type="checkbox" id="agreementChecker-2" name="agreeCheck-2" value="45" class="mandatory agreeChecker">
<input type="checkbox" id="agreementChecker-3" name="agreeCheck-3" value="52" class="mandatory agreeChecker">
$('.agreeChecker').click(function(){
if($(this).prop('checked')){
var resultObj = $(this).val();
var outputObj = $('input[name="agreement_ids"]');
var stringToAppend = resultObj.val().length > 0 ? resultObj.val() + "," : "";
resultObj .val( stringToAppend + outputObj.val() );
}else{
if($('.agreement-text').length > 0){
$('input[name="agreement_ids"]').val('-1');//uncheck
}else{
$('input[name="agreement_ids"]').val('');
}
}
});
Aucun commentaire:
Enregistrer un commentaire