[英]How to get values from two textboxes and display on third on button click using Javascript?
[英]How to delete values from two textboxes by using JavaScript?
我有三個文本框(多行),如果第一個文本框中有第二個文本框數據,則這些數字將被刪除。 喜歡
t1:123456,12345678,9898998,4545454,
t2:123456,66666666
當我按下過濾器按鈕時,第三個文本框數據將是
t3:12345678,9898998,4545454,
碼:
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(',');
}
但是當我進入時;
在數字之后,將需要;
以及數字。
如果我輸入逗號( ,
)或分號( ;
)應該接受這些,並進行過濾。
我將其分解為數組,進行迭代,然后重新創建字符串。 這樣可以避免使用簡單的字符串替換來管理逗號的麻煩。 這是代碼:
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);
這是一個小提琴: http : //jsfiddle.net/82etmett/1/
var input = source.value.trim().replace(/\s/g, '').replace(/;/g,',').split(',');
只是想念replace(/;/g,',')
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.