javascript jQuery从逗号分隔列表中删除值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4587955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:13:26  来源:igfitidea点击:

jQuery removing values from a comma separate list

javascriptjquery

提问by TheExit

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:

在没有逗号问题的情况下,删除 9、4 或 3 之类的值的最佳方法是什么,我不希望这样结束:

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

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

有没有一种干净的方法可以在 JavaScript/jQuery 中完成这项工作?

回答by The Who

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);

回答by Marek Sebera

Similar to PHP implode/explode functions

类似于 PHP 内爆/爆炸功能

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

文档:
fce:拆分
fce:加入
fce:Array.remove

回答by Kirk Strobeck

No jQuery required :P

不需要 jQuery :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>

回答by Yuri Ghensev

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

您也可以使用纯 javascript。假设您只想起飞“4”:

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

or "3" and "9":

或“3”和“9”:

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

回答by Mohammad Reza Arani Kashani

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;
}

回答by Keith

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

回答by Joe Hanink

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;
}

回答by dqhendricks

use

利用

array = string.split(separator);

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

将字符串分解为数组。然后在操作后使用它来加入。

string = array.join(separator);

回答by thekoehl

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

我认为此功能适用于您要执行的操作:http: //www.w3schools.com/jsref/jsref_split.asp

string.split(separator, limit)