簡體   English   中英

使用JavaScript將多個輸入字段中的多個值復制到一個輸入字段中?

[英]copy Multiple values from multiple input fields into one input field using javascript?

我試圖找到一種方法將多個輸入字段的值復制到1個單個輸入字段中。

目前,我只能使用以下代碼將1個輸入字段復制到另一個輸入字段中:

addEvent(document.getElementById('inputName'), 'keyup', function () {
   document.getElementById('inputURL').value = this.value.replace(' ', ' ');
 $(".sect2 input[@value='']").parents(".secTxt").hide();
});


function addEvent(ele, evnt, funct) {
  if (ele.addEventListener) // W3C
    return ele.addEventListener(evnt,funct,false);
  else if (ele.attachEvent)  // IE
    return ele.attachEvent("on"+evnt,funct);
}

是否可以做這樣的事情:

addEvent(document.getElementById('inputName, inputName2, inputName3, inputName4'), 'keyup', function () {
   document.getElementById('inputURL').value = this.value.replace(' ', ' ');
 $(".sect2 input[@value='']").parents(".secTxt").hide();
});


function addEvent(ele, evnt, funct) {
  if (ele.addEventListener) // W3C
    return ele.addEventListener(evnt,funct,false);
  else if (ele.attachEvent)  // IE
    return ele.attachEvent("on"+evnt,funct);
}

如果沒有,正確的方法是什么?

提前致謝。

更新:

以下示例均無效果,也不實用。

我也嘗試了自己的解決方案,但也沒有用。

我只能假設這無法使用javascript!

您的第二個示例是不可能的,但是如果您仍在使用jQuery,則類似的內容可能是最簡單的解決方案

更新 -考慮到這一點,您可能想要以下類似的東西

$(function(){
   $('#inputName, #inputName2, #inputName3, #inputName4').keyup(function(){    
       var str = "";
       $('#inputName, #inputName2, #inputName3, #inputName4').each(function() {
           str += $(this).val().replace(' ', '');
       });
       $('#inputURL').val(str);
   });
});

我在這里添加了一個工作示例的jsFiddle

document.getElementById()插入中,您可以使用document.querySelector()

document.querySelector('#inputName, #inputName2, #inputName3, #inputName4')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM