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

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

I have many checkboxs as below,

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

      Or you use can do using array like shown below

       var checkBoxArray = new Array();
       var areaofinterest = '';
      
       $(':checkbox:checked').each(function(i){
              checkBoxArray .push($(this).attr("value"));
      
      });
      
        if (checkBoxArray .length == 0) {
           } else {
           while (checkBoxArray .length != 0) {
                  if (checkBoxArray .length == 1) {
                      areaofinterest += checkBoxArray .pop();
                  } else {
                      areaofinterest += (checkBoxArray .pop() + ",");
                       }
                  }
      }
      console.log(areaofinterest);
      

      though you will get String in reverse order.

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