javascript - How to get the value of the multi-select box for splicing and remove the comma after the unchecked options
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-07-05 10:51:54
0
5
938

The XML template given to me by the background:
<Pay>0,1,2</Pay>

<input type="checkbox" name="PayTypeList" class="PT1">1
<input type="checkbox" name="PayTypeList" class="PT2">2
<input type="checkbox" name="PayTypeList" class="PT3">3

<script>
($(".PT1").is(':checked')==true)?PT1="0":PT1="";
($(".PT2").is(':checked')==true)?PT2="1":PT2="";
($(".PT3").is(':checked')==true)?PT3="2":PT3="";
</script>
$("#textarea").html("<Pay>"+PT1+","+PT2+","+PT3+"</Pay>")

I use this method to determine and then splice, but if the last option is not selected, then the previous option is followed by a comma, what should I do

曾经蜡笔没有小新
曾经蜡笔没有小新

reply all(5)
阿神

Declare an array. If a checkbox is selected, push the value into it, and finally use join to convert it into a comma-separated array.

var selected = []
if ($(".PT1").is(':checked')) selected.push('1')
// ... ...
$("#textarea").html("<Pay>"+ selected.join() + "</Pay>")

The regular expression used upstairs can only remove the last comma. If there are unselected ones in front, there will still be extra commas.

巴扎黑
var str = "<Pay>"+PT1+","+PT2+","+PT3+"</Pay>";
$("#textarea").html(str.replace(/,<\/Pay>$/, '</Pay>'))
曾经蜡笔没有小新
    var array = new Array();
    $("input").each(function (index) {
        if($(this).is(":checked")) {
            array.push(index);
        }
    });
    $("#textarea").html("<Pay>"+array.join(",")+"</Pay>");
大家讲道理

1. I understand your requirement: you only need to display them in order, regardless of the order of selection. For example, whether you select 2 or 0 first, the display will be 0, 2;

My solution is to use an array to traverse. There are many traversal methods:

  • Option 1: Use reduce (I just learned reduce today, let’s play with it)

<script>
var arr = [PT1,PT2,PT3];
var res = arr.reduce(function(pre,cur,arr){  
  if(pre!=''&&cur!=''){ return pre + ',' + cur;}
  else if(pre!=''&&cur==''){return pre;}
  else if(pre=='' && cur!=''){return cur;}
  else{return '';}
 })
$("#textarea").html("<Pay>"+res+"</Pay>")
</script>
  • Option 2: Use filter + join

<script>
var arr = [PT1,PT2,PT3];
var newarr = arr.filter(function(v,i){ 
    return v==''?false:true;
 })
var res = newarr.join(',');
$("#textarea").html("<Pay>"+res+"</Pay>")
</script>
  • Option 3: Use for loop

<script>
var arr = [PT1,PT2,PT3];
var res = '';
for(var i=0;i<arr.length;i++){
if(arr[i]!=''){

 if(res!=''){

   res += "," + arr[i];
 }else{
   res += arr[i];
}
}else{
  continue;
}
}

$("#textarea").html("<Pay>"+res+"</Pay>")
</script>

2. If you consider the click order, you can use the change event: when the CheckBox changes, judge checked to add or delete data

html:

<input type="checkbox" name="PayTypeList" value='0' class="PT1">1
<input type="checkbox" name="PayTypeList" value='1' class="PT2">2
<input type="checkbox" name="PayTypeList" value='2' class="PT3">3

javascript

var select = [];
$('[name=PayTypeList]').change(function(e){
    if(e.target.checked){
        select.push(e.target.value);
    }else{
        select = select.filter(function(v,i){
            return v != e.target.value;
        })
    }
$("#textarea").html("<Pay>"+ select.join +"</Pay>")
})

(Learned from huguangju’s method of creating an empty array)

淡淡烟草味

As an array, remove the null items in the array and then format it into a string

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template