如何使用Javascript删除多行文本框中的重复项? [英] How to Remove Duplicates in Multiline Textbox by using Javascript?
问题描述
我有两个多行文本框,在第一个文本框中我输入一些带有重复项的数字,现在我在第二个文本框中输入数字。当我按下过滤器按钮时,我得到两个文本框中可用的值,如
textbox1
9900990099,9977889977,9900887766
textbox2
9900990099,1234567890,0987654321
当我按下过滤器按钮我得到
9900990099(两个文本框都有)
如何在Javascript中执行此操作。请帮我
I Have Two Multiline Textboxes and in first textbox i enter some numbers with duplicates also and now i enter numbers in second textbox.when i press filter button i get the values which are available in both the textboxes like
textbox1
9900990099,9977889977,9900887766
textbox2
9900990099,1234567890,0987654321
whn i press filter button i get
9900990099 ( both the textboxes are have)
how can i do this in Javascript. pls help me
推荐答案
试试下面的代码。它适用于ASP.NET;从您的评论中,我看到您使用了它。
Try the below code. It works for ASP.NET; from your comment, I saw you used that.
var txt1 = document.getElementById('<%= txt1.ClientID %>').value;
var txt2 = document.getElementById('<%= txt2.ClientID %>').value;
var txt1values = txt1.split(",");
var txt2values = txt2.split(",");
var commonValues = [];
for (var i = 0; i < txt1values.length; i++) {
var curr = txt1values[i];
if (txt2values.indexOf(curr) != -1) {
commonValues.push(curr);
}
}
var commonValuesStr = commonValues.join(",");
document.getElementById('<%= txt3.ClientID %>').value = commonValuesStr;
这是如何工作的? ASP.NET服务器通过可在客户端使用的ID替换<%= txt1.ClientID%>
。然后,代码用逗号分隔文本。然后,它循环遍历第一个文本框的所有值。该值是否也在第二个文本框的值中?然后它被添加到 commonValues
。最后, commonValues
中的所有值都以逗号连接。
How does this work? The ASP.NET server replaces <%= txt1.ClientID %>
by the ID that can be used on the client side. Then, the code splits the text by a comma. Then, it loops over all values of the first textbox. Is that value also in the values of the second textbox? Then it gets added to commonValues
. At the end, all values in commonValues
get joined with a comma.
这篇关于如何使用Javascript删除多行文本框中的重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!