简体   繁体   English

Javascript-比较2个输入拆分数组并输出差异

[英]Javascript - Compare 2 input split arrays and output the difference

I have 2 inputs both containing a list of numbers separated by a '|'. 我有2个输入,都包含一个用'|'分隔的数字列表。 I need to check if any of the ids from the first input exist in the second and if it doesn't add it to a third. 我需要检查第二个输入中是否存在来自第一个输入的任何ID,以及是否没有将其添加到第三个输入中。

<input type="text" id="access_ids" value="13|16|24|25|31|33|36|42|43|45|48|49|58|59|61|8" /><br />
<input type="text" id="replied_ids" value="8|9|16" /><br />
<input type="text" id="not_replied_ids" value="" />

.

$(document).ready(function(){
var acc_ids = $('#access_ids').text();
var acc_array = acc_ids.split('|');

for (var i = 0; i < acc_array.length; i++) {
    if (acc_array[i].indexOf($('#replied_ids')) > -1) {
        $('#not_replied_ids').text(acc_array[i].join('|'));
        return;
    }
}

}); });

I made a jsfiddle: https://jsfiddle.net/sheferd/nhj63fbu/1/ 我做了一个jsfiddle: https ://jsfiddle.net/sheferd/nhj63fbu/1/

Thanks 谢谢

You are wrong syntax, ex: $('#access_ids').text() --> $('#access_ids').val() ... You can try follow code: 您语法错误,例如: $('#access_ids').text() -> $('#access_ids').val() ...您可以尝试按照以下代码操作:

$(document).ready(function(){
    var acc_ids = $('#access_ids').val();
    var acc_array = acc_ids.split('|');
  var not_replied_arr = [];
    for (var i = 0; i < acc_array.length; i++) {
        if ($('#replied_ids').val().indexOf(acc_array[i]) == -1) {
            not_replied_arr.push(acc_array[i]);
            $("#not_replied_ids").val(not_replied_arr.join("|"));
            return;
        }
    }
}); 

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

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