繁体   English   中英

从javascript中的收件箱值中删除特定值

[英]remove particular value from an inbox box value in javascript

我想从具有匹配值的输入框中删除值。

   onclick(value){
    var array = document.getElementById('related-products-field').value;
     alert(array);
    //array = 339,340,339,340,338 
    // if(value==339) ,339 should be remove

     }

您可以使用正则表达式,或者如果您正在寻找特殊字符,例如:

string.indexOf(yourInput) !== -1;

要么

var entryArray = array.split(',');
var output =entryArray.filter(el=>el!==parseInt(input));

您可以使用如下所示的filter

 var array=[1,2,3,4,5,6]; function f(){ y=array.filter(x=>x!==parseInt(document.getElementById("hi").value)); console.log(y); } 
 <input id="hi" type="number"> <button onclick="f()">click</button> 

请参阅下面的内联评论:

 var txt = document.getElementById("test"); var vals = [339,340,338]; txt.addEventListener("change", function(){ // Cache a reference to the input that triggered the event var self = this; // Loop over the array vals.forEach(function(item){ // Convert the field value to a number and compare against the array item if(+self.value === item){ // Remove the value from the field self.value = self.value.replace(item, ""); } }); }); 
 <p>Type in a value and then hit TAB<p> <input type="text" id="test"> 

为了获得预期的结果,请使用以下使用数组拼接的选项

  var remove = function(value){ var array = document.getElementById('related-products-field').value; arr = [339,340,338] console.log(arr.indexOf(parseInt(array))) arr.splice(arr.indexOf(parseInt(array)),1) alert(arr); } 
 <input type="text" id="related-products-field" > <button onclick="remove()">remove</button> 

代码示例-https: //codepen.io/nagasai/pen/oqjZYM?editors=1011

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM