2017-07-18 94 views
1

我使用普通JS動態創建表單。每當最後一個輸入字段收到一個輸入時,應該創建另一個空的輸入。另一方面,當最後一個字段變爲空時,最後一個字段應該被刪除。當應用於多個元素時觸發事件僅一次

HTML(之前用戶輸入):

<div id="eventList" class="gap"> 
     <input id="event1" class="text bold eventBox" type="text" name="event1" placeholder="Ereignis 1" /> <br> 
</div> 

的Javascript(後實施;在摘錄):

function addBox() { 

    var boxes = document.getElementsByClassName("eventBox"); 

    var id = +getMaxID(boxes, "event") + +1;   // ID of input being created 

    var list = document.getElementById("eventList"); 

    list.innerHTML = list.innerHTML + '<input id="event'+ id +'" class="text bold eventBox" type="text" name="event'+ id +'" placeholder="Ereignis '+ id +'" /> <br>'; 

    document.getElementById("event" + id).addEventListener("input", resizeEventList); 

} 

function delBox() { 

    loadTexts();    // save current inputs 

    var list = document.getElementById("eventList"); 

    document.getElementById("eventList").innerHTML = ''; 

    var id = 1; 

    for (var i = 1; i < eventTexts.length; i++) { 

     if (eventTexts[i].length > 0) { 

      /* recreate box with its former input (if not empty) */ 

      document.getElementById("eventList").innerHTML = document.getElementById("eventList").innerHTML + '<input id="event'+ id +'" class="text bold eventBox" type="text" name="event'+ id +'" placeholder="Ereignis '+ id +'" value="' + eventTexts[i] + '"/> <br>'; 
      document.getElementById("event" + id).addEventListener("input", resizeEventList); 

      id++; 

     } 

    } 

    /* add new last empty input */ 

    document.getElementById("eventList").innerHTML = document.getElementById("eventList").innerHTML + '<input id="event'+ id +'" class="text bold eventBox" type="text" name="event'+ id +'" placeholder="Ereignis '+ id +'" value=""/> <br>'; 
    document.getElementById("event" + id).addEventListener("input", resizeEventList); 

} 

function resizeEventList(e) { 

    if (typeof e != 'undefined') { 

     var boxes = document.getElementsByClassName("eventBox"); 

     if (e.target.id == "event"+getMaxID(boxes, "event") && e.type == 'input') { 

      /* input into last available box -> add an empty box */ 

       loadTexts();    // save current inputs 

       senderID = e.target.id;  // remember ID of triggering element for resetting focus afterwards 

       addBox();     // add another box 

       writeTexts();    // write former inputs back into boxes 

       document.getElementById(senderID).focus();  // reset focus 

     } else if (e.target.value.length == 0 && getID(e.target, "event") < getMaxID(boxes, "event")) { 

      /* box before last box empty -> delete last empty box */ 

       senderID = e.target.id;  // remember ID of triggering element for resetting focus afterwards 

       delBox();     // delete all boxes and create X new boxes 

       document.getElementById(senderID).focus();  // reset focus 

     } 

    } 

} 

var eventTexts = new Array(); 
var senderID; 
document.getElementById("event1").addEventListener("input", resizeEventList); 

現在的問題:當我申請的所有事件監聽器只名爲輸入「事件1「,代碼按預期工作 - 打字時添加輸入,空白時刪除,僅適用於event1。當我將事件偵聽器也應用於動態創建的輸入時(應該是這樣,並且將其寫入上面的代碼中),輸入事件在有史以來的第一個輸入中只觸發一次,包括event1。以下所有輸入不再記錄。

有沒有人有解決方案?

感謝,Jaspa

---全碼--------------------

<form id="planForm" class="top-margin" method="post" action="new.php?action=add"> 

    <div> 
     <input class="text head bold" type="date" name="date" placeholder="Datum" value="<?php echo date("Y-m-d", time());?>" required /> <br> 

     <div id="eventList" class="gap"> 

      <input id="event1" class="text bold eventBox" type="text" name="event1" placeholder="Ereignis 1" /> <br> 

     </div> 
    </div > 

<input type="button" value="Speichern" onclick="save();"/> 

</form> 

<script> 

    function getID(elm, type) { 
     if (typeof elm != 'undefined') { 
      return elm.id.replace(type, ""); 
     } else { 
      return ""; 
     } 
    } 

    function getMaxID(list, type) { 

     currID = 0; 
     max = 0; 

     if (list != null && list.length > 0 && type != null) { 
      for (var i=0; i < list.length; i++) { 
       currID = getID(list[i], type); 
       if (currID > max) { 
        max = currID; 
       } 
      } 
     } 

     return max; 

    } 

    function addBox() { 

     var boxes = document.getElementsByClassName("eventBox"); 

     var id = +getMaxID(boxes, "event") + +1;   // ID of input being created 

     var list = document.getElementById("eventList"); 

     list.innerHTML = list.innerHTML + '<input id="event'+ id +'" class="text bold eventBox" type="text" name="event'+ id +'" placeholder="Ereignis '+ id +'" /> <br>'; 

     document.getElementById("event" + id).addEventListener("input", resizeEventList); 

     console.log(document.getElementById("event" + id)); 

    } 

    function delBox() { 

     loadTexts();    // save current inputs 

     var list = document.getElementById("eventList"); 

     document.getElementById("eventList").innerHTML = ''; 

     var id = 1; 

     for (var i = 1; i < eventTexts.length; i++) { 

      if (eventTexts[i].length > 0) { 

       /* recreate box with its former input (if not empty) */ 

       document.getElementById("eventList").innerHTML = document.getElementById("eventList").innerHTML + '<input id="event'+ id +'" class="text bold eventBox" type="text" name="event'+ id +'" placeholder="Ereignis '+ id +'" value="' + eventTexts[i] + '"/> <br>'; 
       document.getElementById("event" + id).addEventListener("input", resizeEventList); 

       id++; 

      } 

     } 

     /* add new last empty input */ 

     document.getElementById("eventList").innerHTML = document.getElementById("eventList").innerHTML + '<input id="event'+ id +'" class="text bold eventBox" type="text" name="event'+ id +'" placeholder="Ereignis '+ id +'" value=""/> <br>'; 
     document.getElementById("event" + id).addEventListener("input", resizeEventList); 

    } 

    function resizeEventList(e) { 

     if (typeof e != 'undefined') { 

      var boxes = document.getElementsByClassName("eventBox"); 

      if (e.target.id == "event"+getMaxID(boxes, "event") && e.type == 'input') { 

       /* input into last available box -> add an empty box */ 

        loadTexts();    // save current inputs 

        senderID = e.target.id;  // remember ID of triggering element for resetting focus afterwards 

        addBox();     // add another box 

        writeTexts();    // write former inputs back into boxes 

        document.getElementById(senderID).focus();  // reset focus 

      } else if (e.target.value.length == 0 && getID(e.target, "event") < getMaxID(boxes, "event")) { 

       /* box before last box empty -> delete last empty box */ 

        senderID = e.target.id;  // remember ID of triggering element for resetting focus afterwards 

        delBox();     // delete all boxes and create X new boxes 

        document.getElementById(senderID).focus();  // reset focus 

      } 

     } 

    } 

    function loadTexts() { 
     var boxes = document.getElementsByClassName("eventBox"); 

     for (var i=0; i < boxes.length; i++) { 
      eventTexts[getID(document.getElementById(boxes[i].id), "event")] = boxes[i].value; 
     } 
    } 

    function writeTexts() { 
     var boxes = document.getElementsByClassName("eventBox"); 

     for (var i=0; i < boxes.length-1; i++) { 
      document.getElementById(boxes[i].id).value = eventTexts[getID(boxes[i], "event")]; 
     } 
    } 

    var eventTexts = new Array(); 
    var senderID; 
    document.getElementById("event1").addEventListener("input", resizeEventList); 

</script> 
+0

你註冊了,你實際上在添加監聽器之前獲取頁面上的元素? – mhatch

+0

是的,我得到正確的ID,但只在第一次輸入之後。進一步的輸入根本不觸發事件 – Jaspa

+0

你看到這是否返回元素? document.getElementById(「event」+ id) –

回答

相關問題