【问题标题】:Compare two text inputs with comma seperated values return true if one input contains only values that are inside the other input如果一个输入仅包含另一个输入内的值,则将两个文本输入与逗号分隔值进行比较返回 true
【发布时间】:2020-11-26 16:33:00
【问题描述】:

当您有两个都具有逗号分隔值的输入时。我如何比较这些以便我可以得到一个if match 然后这样做else 这样做。

<input type='text' class='focusfield' value=''>

<input type='text' class='fixed' value='4002,4003,4004,4005,4006,4007'>


// Example 1 for generated input (should return FALSE)
<input type='text' class='generated' value='3500,3700,4002,4006,4007,4005,4004,4003'>

// Example 2 for generated input (should return FALSE)
<input type='text' class='generated' value='3500'>

// Example 3 for generated input (should return TRUE)
<input type='text' class='generated' value='4002,4006,4007,4005,4004,4003'>

// Example 4 for generated input (should return TRUE)
<input type='text' class='generated' value='4002'>

注意generated 输入的顺序与fixed 字段的顺序不同。

当输入generated 中的所有数字 也出现在输入fixed 中时,它应该返回true,否则它应该返回false。离开focusfield时必须运行此代码。

在文档就绪函数中,我有以下内容(这是我尝试过的所有内容中唯一有效的部分,请参阅以下链接):

$('.focusfield').focusout(function(){

});

我已经尝试在我的代码中使用以下答案,但它们都没有提供结果,通常我可以通过现有的 SO 问题得到接近我想要的结果,但这次我没有。所以我没有部分工作代码可以开始。

how to compare value with comma separated value in javascript or jquery

how to compare two currency in jquery with comma

jquery compare two fields values

compare two input values using Jquery

comparing two strings with comma separated values(用于 c+,但我可以从中得到一个想法,但遗憾的是我没有。)

multiple comma seperate value compare using JS

我的历史中有更多链接,但我认为这不会增加我只想发布这些链接的问题,以便每个人都知道我不只是在这里要求免费代码;-0。

感谢所有帮助并花时间阅读所有内容的人。

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    让我们用最少的代码工作:

    const goal = '4002,4003,4004,4005,4006,4007';
    const test ='3500,3700,4002,4006,4007,4005,4004,4003';
    

    如果每个测试编号都包含在目标中,您需要一个返回 true 的方法。

    function inside(a, b) {
      //array of numbers
      const aV = a.split(',').map( e => +e);
      const bV = b.split(',').map( e => +e);
    
      //every b must be in a
      return bV.every( v => aV.indexOf(v) !== -1 );
    }
    

    const goal = '1,2,3';
    const test ='1,2';
    
    function inside(b, a) {
      //array of numbers
      const aV = a.split(',').map( e => +e);
      const bV = b.split(',').map( e => +e);
    
      //every b must be in a
      return bV.every( v => aV.indexOf(v) !== -1 );
    }
    
    console.log('Is', test);
    console.log('inside', goal);
    console.log('=>', inside(test, goal) );

    版本

    这里是一步一步的解决方案 您需要一个接受两个字符串并返回一个布尔值的函数。

    function inside(a,b) {
        return true;
    }
    

    使用 'array as string coma separator' 很痛苦...让我们将字符串转换为数组,用 ',' 分割

    function inside(b, a) {
      //array of numbers
      const aV = a.split(',');
      const bV = b.split(',');
      return true
    }
    

    然后我从字符串数组更改为数字数组,使用.map( e =&gt; +e),这完全没用哈哈......对不起......所以让我们忘记这一点

    好的,所以我们要确保 Bv 中的每个值都在 aV 中。 因此,对于像 x 这样的 Bv 中的给定值,我们要测试 x 在 aV 中。让我们检查一下 aV 中 x 的索引:aV.indexOf(x) 如果是-1,则表示not present...

    每一个 bV 都很棒!!!

    function inside(b, a) {
      //array of numbers
      const aV = a.split(',');
      const bV = b.split(',');
    
      for( let i = 0; i < bV.length; i++) {
        const x = bV[i];
        const index = aV.indexOf(x);
    
        if ( index === -1 ) {
            //Not present, we stop here
            return false;
        }
      }
    
      //So we check for every Bv and none were absent, let's return true
      return true;
    }
    

    好的,但是...我不喜欢将 for 与 Array 一起使用,让我们与 forEach 一起使用

    function inside(b, a) {
      //array of numbers
      const aV = a.split(',');
      const bV = b.split(',');
    
      //Let's define everything is ok
      let result = true;
    
      bV.forEach( x => {
        if ( aV.indexOf(x) === -1 )
            result = false;
      })
    
      return result;
    }
    

    很好,但这正是 every 方法的重点!

    function inside(b, a) {
      //array of numbers
      const aV = a.split(',');
      const bV = b.split(',');
    
      return bV.every( x => {
        if ( aV.indexOf(x) === -1 )
            result = false;
        return true;
      })
    }
    

    让我们简而言之

    function inside(b, a) {
      //array of numbers
      const aV = a.split(',').map( e => +e);
      const bV = b.split(',').map( e => +e);
    
      //every b must be in a
      return bV.every( v => aV.indexOf(v) !== -1 );
    }
    

    Array.everyString.splitArray.indexOf

    【讨论】:

    • 我刚刚在小提琴中进行了测试,它运行良好。您是否可以向我指出一些文档,我可以在其中逐步学习此代码,或者指出一些要使用的搜索参数。或者,如果您愿意添加更多的 cmets,那就更好了。尽管如此,我还是要感谢你,因为你的代码结束了我令人沮丧的几个小时。所以非常感谢!
    • 我添加了更详细的解决方案
    • 哇,这正是我要读十几遍的意思。这太棒了,非常感谢!这让我更容易理解你做了什么。我从未有过如此详细的答案。谢谢!!!
    【解决方案2】:

    您可以使用几个内置的 javascript 函数来做到这一点。

    您首先拆分值,将它们存储在数组中并对值进行排序。 你可以比较这个数组的元素是否等于源数组。

    我制作了一个 jsfiddle 来解释它。 https://jsfiddle.net/3cLa5614/

    function checkValues(input) {
        const sources = document.getElementById('source').value.split(',').sort();
        const values = input.value.split(',').sort();
    
        let comparator = sources.splice(0,values.length);
        return JSON.stringify(comparator)==JSON.stringify(values)
    }
    

    【讨论】:

    • 感谢您的意见。然而,我确实已经选择了另一个答案,并且我坚持这个选择,但我很欣赏你的回答并支持它,我相信你会在未来几年获得更多支持。
    猜你喜欢
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多