简体   繁体   中英

How to delete values from two textboxes by using JavaScript?

I have three textboxes (multiline), if the second textbox data is available in first one those numbers will be deleted. like

t1:123456,12345678,9898998,4545454,
t2:123456,66666666

When I press filter button the third textbox data will be

t3:12345678,9898998,4545454,

Code:

function GetDistinctElements(source, source1, target) {
    var input = source.value.trim().replace(/\s/g, '').split(',');
    var input1 = source1.value.trim().replace(/\s/g, '').split(',');
    input = input.filter(function (val) {
        return input1.indexOf(val) == -1;
    });
    var distinctArray = input.filter(function (item, pos) {
        return input.indexOf(item) == pos;  
    });
    target.value = distinctArray.join(',');
}

But when I enter ; after the digit, it will take ; as a number as well.

If I enter comma ( , ) or semicolon ( ; ) it should accept those, and filter will be performed.

I would explode it to an array, iterate, and then recreate the string. This avoids problems the managing the commas that you would have with a simple string replace. Here's the code:

 var val1 = $("#1").val(); var val2 = $("#2").val(); var array1 = val1.split(","); var array2 = val2.split(","); var val3 = ""; var count = 0; for (var i = 0; i < array1.length; i++) { if (!($.inArray(array1[i], array2) >= 0)){ if (count > 0) val3 = val3 + ","; val3 = val3 + array1[i]; count++; } } $("#3").val(val3); 

Here's a fiddle: http://jsfiddle.net/82etmett/1/

var input = source.value.trim().replace(/\s/g, '').replace(/;/g,',').split(',');

只是想念replace(/;/g,',')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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