如何在javascript中删除重复值 [英] How to remove Duplicate value in javascript

查看:60
本文介绍了如何在javascript中删除重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var counter = 0;

            var arr = new Array();

            function addFUControl() {
                
                var visible = $('input[type=file]').filter(function () {
                    return !($(this).css('visibility') == 'hidden' || $(this).css('display') == 'none');
                }).length;

                if (visible <= 1) {
                    for (var i = 0; i <= 4; i++) {
                        AddFileUpload();
                    }
                }
                else {

                }
            }


            function AddFileUpload() {
                var div = document.createElement('DIV');
                div.innerHTML = '<input id="file_' + counter + '" name = "file' + counter +
                 '" type="file" Class = "fileUploadCntrl" />';

                document.getElementById("FileUploadContainer").appendChild(div);


                arr[counter] = document.getElementsByName("file" + counter);
                counter++;

                //document.getElementById("FileUploadContainer").removeChild(div);

            }


            function FileUploadClick() {

                $find('<%= modeloPopUpFileUpload.ClientID%>').hide();
                debugger;
                for (var i = 0; i < arr.length; i++) {
                    var previousFilePath = $(arr[i - 1]).val();                    
                    var filepath = $(arr[i]).val();

                    if (filepath != "")
                    {
                        if (filepath != previousFilePath) {
                            var Button = '<input id="btn_' + i + '" name = "btn' + i +
                                         '" type="button" text="Delete" Class = "btn" style=width:80px; />';

                            $("#SelectedFilesManuplation").append(filepath);
                            $("#SelectedFilesManuplation").append(Button);
                            $("#SelectedFilesManuplation").append('<br>')                            
                            $("#file1Flag").val("1")
                        }
                        var a = arr[i];
                        $(a).hide();
                    }
                    else if (filepath == "")
                    {
                        var p2 = arr[i];
                        $(p2).remove();
                    }





这些是我的功能。



when用户点击btn_AddFileUploadControl一个弹出窗口打开并调用函数addFUControl()。



和函数在弹出窗口中创建5个动态FileUpload控件。



用户选择文件,然后按btn_OK。在btn_OK上单击它调用函数FileUploadClick()。



这将关闭弹出窗口并在我的页面上显示文件路径。



隐藏有值的控件。并删除那些没有文件的控件。



我的问题是这个...... />




突然我的用户想要添加更多文件。当他点击btn_AddFileUploadControl时,弹出窗口打开,用户再选择1个文件。



然后他按btn_OK。

上次点击btn_OK上隐藏的文件/控件。

正在重复再次在页面中。



例如。第一次用户选择

1:c:/> test1.text

2:c:/> test2.text

这两个文件。这些文件路径显示在页面上。



当他再次点击btn_AddFileUploadControl并从弹出窗口中再选择一个文件



3:c:/> test3.text



wh他按btn_OK然后在我的页面上显示重复。

喜欢这个



c:/> test1.text

c:/> test2.text

c:/> test1.text

c:/> test2.text



c:/> test3 .text





但是我不想要这个。



i想要show



c:/> test1.text

c:/> test2.text

c:/> ; test3.text







请帮我解决这个问题。



These are my functions.

when User click on btn_AddFileUploadControl a popup get open and call the function addFUControl().

and the function create 5 dynamically FileUpload controls in the popup.

User select file and then press btn_OK. on btn_OK click it call the function FileUploadClick().

and this close the popup and display the filepath on my page.

hide the controls which have value.and remove those controls which don't have files.

MY PROBLEM IS THIS....


suddenly my user want to add more file.when he click on btn_AddFileUploadControl the popup get open and user select 1 more file.

and then he press btn_OK.
the files/controls which got hidden on btn_OK click last time.
are repeating again in the page.

for example.first time user select
1: c:/>test1.text
2: c:/>test2.text
these two files.and these file path displaying on page.

and when he click on again btn_AddFileUploadControl and from popup he select one more file

3: c:/>test3.text

when he press btn_OK then on my page it is displaying repetition.
like this one

c:/>test1.text
c:/>test2.text
c:/>test1.text
c:/>test2.text

c:/>test3.text


but i dont want this.

i want to show

c:/>test1.text
c:/>test2.text
c:/>test3.text



please help me in this problem.

推荐答案

' input [type = file]')。 ( function (){
return !(
('input[type=file]').filter(function () { return !(


this )。css(' visibility')== ' hidden' ||
(this).css('visibility') == 'hidden' ||


this )。css(' display')= = ' none');
})。长度;

if (可见< = 1 ){
< span class =code-keyword> for ( var i = 0 ; i< ; = 4 ; i ++){
AddFileUpload();
}
}
其他 {

}
}


function AddFileUpload(){
var div = document .createElement(' DIV');
div.innerHTML = ' < input id =file _' + counter + ' name =file' + counter +
' type =fileClass =fileUploadCntrl/>';

document .getElementById( FileUploadContainer)的appendChild(DIV)。


arr [counter] = document .getElementsByName( file + counter);
counter ++;

// document.getElementById(FileUploadContainer)。removeChild(div);

}


function FileUploadClick(){
(this).css('display') == 'none'); }).length; if (visible <= 1) { for (var i = 0; i <= 4; i++) { AddFileUpload(); } } else { } } function AddFileUpload() { var div = document.createElement('DIV'); div.innerHTML = '<input id="file_' + counter + '" name = "file' + counter + '" type="file" Class = "fileUploadCntrl" />'; document.getElementById("FileUploadContainer").appendChild(div); arr[counter] = document.getElementsByName("file" + counter); counter++; //document.getElementById("FileUploadContainer").removeChild(div); } function FileUploadClick() {


这篇关于如何在javascript中删除重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆