Put checkbox values into hidden input with jQuery

前端 未结 2 1573
执念已碎
执念已碎 2021-01-14 23:08

I\'d like to populate a hidden input with the values of selected checkboxes with a space between these values. I\'ve tried the following which in theory should work but it i

2条回答
  •  野的像风
    2021-01-14 23:33

    You just need to change your selector and attach your script to some event. For example:

    $('input[type=checkbox]').change(function() {
    
        var vals = $('input[type=checkbox]:checked').map(function() {
            return $(this).val();
        }).get().join(',');
    
        $('#tags').val(vals);
    
    });​
    

    See this DEMO.

提交回复
热议问题