jQuery convert checkbox selections to array

后端 未结 2 738
闹比i
闹比i 2021-01-27 12:08

I have an HTML like this:

  • 2条回答
    •  故里飘歌
      2021-01-27 12:52

      You want to store/send the selected values in one array, right? So why you use unique names then for each checkbox?! ... if they belong together then name them appropriately like so:

          
          
          
          
          
      
          
          
          //....
      

      Then they will be sent as an array on submit automatically, no further rearrangement needed ... or if you really want to do it the ajax way still, you can easily get all the ids for the category-checkboxes like so:

          $('[name="pgggo-category-sep[]"]').on('change', function () {
            let values = Array.from($('[name="pgggo-category-sep[]"]:checked'))
              .map(elem => $(elem).val())
          })
      

    提交回复
    热议问题