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

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

I have many checkboxs as below,

  • Coo
  • 相关标签:
    7条回答
    • 2020-12-23 17:37

      Try something like this:

      var areaofinterest= "";
      
      $("input[type=\"checkbox\"]").each(function() {
          if ($(this).attr("checked")) {
              if (areaofinterest !== "") areaofinterest += ",";
      
              areaofinterest += $(this).value();
          }
      });
      
      0 讨论(0)
    • 2020-12-23 17:39
      var values = "";
      $("checkbox[name=areaofinterest]").each(function() { 
          values += $(this).val() + ",";
      });
      
      values = values.substring(0, values.length - 2);
      
      0 讨论(0)
    • 2020-12-23 17:43
       var mode = [];
                  jQuery("input[name='mode[]']:checked").each(function(i) {
                      mode.push($(this).val());
                  });
      
      0 讨论(0)
    • 2020-12-23 17:45

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

      var programming = $("input[name='programming']:checked").map(function() {
          return this.value;
      }).get().join(', ');
      
      alert("My favourite programming languages are: " + programming);
      
          OR
      
      var favProgramming = [];
      $.each($("input[name='programming']:checked"), function(){            
          favProgramming .push($(this).val());
      });
      
      alert("My favourite programming languages are: " + favProgramming);
      

      FOR DEMO HERE

      0 讨论(0)
    • 2020-12-23 17:51
      var areaofinterest = '';
      
      $('[name="areaofinterest"]')​.each(function(i,e) {
          var comma = areaofinterest.length===0?'':',';
          areaofinterest += (comma+e.value);
      })​;
      

      To get only checked boxes value :

      var areaofinterest = '';
      
      $('[name="areaofinterest"]')​.each(function(i,e) {
          if ($(e).is(':checked')) {
              var comma = areaofinterest.length===0?'':',';
              areaofinterest += (comma+e.value);
          }
      })​;
      

      FIDDLE

      You could also do:

      var areaofinterest = [];
      $('[name="areaofinterest"]:checked').each(function(i,e) {
          areaofinterest.push(e.value);
      });
      
      areaofinterest = areaofinterest.join(',');
      

      And there's probably a bunch of other ways to do this ?

      0 讨论(0)
    • 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

      0 讨论(0)
    提交回复
    热议问题