开发者

jQuery removing values from a comma separate list

Given an input开发者_如何学运维 like:

<input type="test" value="3,4,9" />

What's the best way to remove a value like 9, 4 or 3, without having issues with the commas, I don't want this ending up:

value="3,4,"
value="3,,9"
value=",4,9"

Is there a clean way to get this done in JavaScript/jQuery?


You could split your value into an array, then filter out values you do not want.

$("input[type='test']").val().split(",") // ["3","4","9"]
.filter(function(v){return !isNaN(parseInt(v))})    // filter out anything which is not 0 or more

Here is a less terse version which filters out anything which is not numeric

var array = $("input[type='test']").val().split(",");

// If you are dealing with numeric values then you will want
// to cast the string as a number
var numbers  = array.map(function(v){ return parseInt(v)});

// Remove anything which is not a number
var filtered = numbers.filter(function(v){ return !isNaN(v)});

// If you want to rejoin your values
var joined = filtered.join(",");

Finally change the value on the input

$("input[type='test']").val(joined);


Similar to PHP implode/explode functions

Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

var explode = value.split(',');
explode.remove(1);
var implode = explode.join(',');

Documentation:
fce: Split
fce: Join
fce: Array.remove


No jQuery required :P

<script type="text/javascript">

    //var subject = '3,4,9';
    //var subject = '3,,9';
    var subject = ',,4,9';

    var clean = Array();
    var i = 0;

    subject = subject.split(',');

    for (var a in subject)
    {
        if(subject[a].length)
        {
            clean[i] = subject[a];
            i++;
        }
    }

    document.write(clean.join(','));

</script>


You may also use pure javascript. Let say you want to take off only "4":

value = value.replace(/4,?/, '')

or "3" and "9":

value = value.replace(/([39],?)+/, '')


I think this function will work for what you are trying to do: http://www.w3schools.com/jsref/jsref_split.asp

string.split(separator, limit)


use

array = string.split(separator);

to break a string into an array. then use this to join after manipulations.

string = array.join(separator);


var ary = value.split(',');
ary.splice(indexOfItemToRemove,1)
var result = ary.join(',');


This is discussed in another post:

remove value from comma separated values string

var removeValue = function(list, value, separator) {
   separator = separator || ",";
   var values = list.split(",");
   for(var i = 0 ; i < values.length ; i++) {
      if(values[i] == value) {
         values.splice(i, 1);
         return values.join(",");
      }
   }
   return list;
}


You can use this function:

function removeComma(x) {
    var str = '';
    var subs = '';

    for(i=1; i<=x.length; i++) {
        subs = x.substring(i-1, i).trim();

        if(subs !== ',') {
            str = str+subs;
        }
    }

    return str;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜