jQuery checkbox values to comma separated list

后端 未结 4 881
南方客
南方客 2021-01-04 03:00

I have several checkboxes with a name array and I want the output of the checked boxes to be a variable with a comma separated list.



        
4条回答
  •  梦谈多话
    2021-01-04 03:36

    Currently un-tested, but I believe the following should work:

    var valuesArray = $('input:checkbox:checked').map( function () {
        return $(this).val();
    }).get().join();
    

    Edited, after a small break, to use native DOM, rather than $(this).val() (which is needlessly expensive, in context):

    var valuesArray = $('input:checkbox:checked').map( function() {
        return this.value;
    }).get().join(",");
    

提交回复
热议问题