简体   繁体   中英

get value of checked checkbox

I'm stacked with getting values from fieldset of checkboxes...Basically i need to fill the value(s) immediatelly each time i check or uncheck a checkbox. And fill values separated by comma in the textarea listed below. I need only checked values (without zero's etc of unchecked) Also without any button for getting data! Input type=checkbox are stored in fieldset id=XXX and textarea where i should store "live" results is also given below with id=YYY

 <fieldset id="XXX" class="checkboxes">
 <input type="checkbox" id="user_chosen_group0" name="user_chosen_group[]" value="14" class="checkbox" size="1">
 <input type="checkbox" id="user_chosen_group1" name="user_chosen_group[]" value="12" class="checkbox" size="1">
 ...</fieldset>

and I need store results in...

 <div id="WWW" class="QQQ">
 <textarea id="YYY" name="user_chosen_group_tag" cols="25" rows="3" class="inputbox textarea validate[maxSize[255]]"></textarea>
 </div>

This should work.

$('input:checkbox').change(function(){
    $('input:checked').each(function(){
           $('#YYY').append($(this).val() + ",");
    });
});

While it's been some time, I thought I'd offer a suggestion:

$('input[type="checkbox"]').change(function(){
    $('#YYY')
        .val($('input[type="checkbox"]:checked')
        .map(function(){
            return this.value;
        }).get().join(', '));
}).change();

JS Fiddle demo .

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM