Using jQuery to get multiple checkbox's value and output as comma separated String.

后端 未结 7 1219
没有蜡笔的小新
没有蜡笔的小新 2020-12-23 17:13

I have many checkboxs as below,

  • Coo
  • 7条回答
    •  伪装坚强ぢ
      2020-12-23 17:52

      Use the .map() function:

      $('.Checkbox:checked').map(function() {return this.value;}).get().join(',')
      

      Breaking that down:

      $('.Checkbox:checked') selects the checked checkboxes.

      .map(function() {return this.value;}) creates a jQuery object that holds an array containing the values of the checked checkboxes.

      .get() returns the actual array.

      .join(','); joins all of the elements of the array into a string, separated by a comma.

      Working DEMO

    提交回复
    热议问题