最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery removing values from a comma separate list - Stack Overflow

programmeradmin3浏览0评论

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 mas, 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?

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 mas, 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?

Share Improve this question edited Sep 20, 2014 at 19:47 BenMorel 36.6k51 gold badges205 silver badges335 bronze badges asked Jan 3, 2011 at 20:14 TheExitTheExit 5,32012 gold badges40 silver badges50 bronze badges
Add a ment  | 

9 Answers 9

Reset to default 7

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./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 ma 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)

  1. 暂无评论