2017-07-19 65 views
0

在我的代碼我使用谷歌地圖API。在這裏我使用onclick方法按鈕。如果我動態點擊它,它會顯示多個文本框。在這裏我輸入所有文本字段的值。但是,當我將參數名稱傳遞到servlet頁面時,它只需要文本框的第一個值。如何獲得所有的價值?如何獲取參數名稱爲數組?

My code 

    var button = document.getElementById('waypoint-input'); 

    button.addEventListener("click", function() { 

     var parentNode = document.getElementById('waypoints-list'); 

     var input = document.createElement('input'); 
     input.type = 'text';  
     input.placeholder = 'Enter a waypoint location'; 
     input.id = 'waypoint' + me.waypointIndex; 
     input.inputId = me.waypointIndex; 
     **input.name = 'waypointlist';** 

     input.addEventListener('input', function() { 

      if (input.value == "") { 

       var waypoint = me.waypts.filter(function (obj) { 
        return obj.key === input.inputId; 
       })[0]; 

       if (waypoint != null && typeof waypoint !== "undefined") { 

        var waypointIndexToRemove = me.waypts.map(function (el) { 
         return el.key; 
        }).indexOf(input.inputId); 

        me.waypts.splice(waypointIndexToRemove, 1); 

        me.route(); 
       } 
      } 
     }); 

     var removeInput = document.createElement('button'); 
     removeInput.innerHTML = 'Remove'; 
     removeInput.onclick = function() { 
      parentNode.removeChild(input); 
      parentNode.removeChild(removeInput); 

      var childInputs = parentNode.getElementsByTagName('input'); 

      if (childInputs.length > 0) { 
       for (var i = 0; i < childInputs.length; i++) { 
        childInputs[i].inputId = i; 
       } 
      } 

      if (input.value != "" && input.value != null) { 

       var waypointIndexToRemove = me.waypts.map(function (el) { 
        return el.key; 
       }).indexOf(input.inputId); 

       me.waypts.splice(waypointIndexToRemove, 1); 

       for (var i = input.inputId; i < me.waypts.length; i++) { 
        me.waypts[i].key = me.waypts[i].key - 1; 
       } 

       me.route(); 
      } 

      me.waypointIndex--; 
     } 

     parentNode.appendChild(input); 
     parentNode.appendChild(removeInput); 

     var waypointAutocomplete = new google.maps.places.Autocomplete(input, { placeIdOnly: true }); 

     me.setupPlaceChangedListener(waypointAutocomplete, 'WAYPOINT', input.inputId); 

     me.waypointIndex++; 

    }, false); 
+0

看起來JavaScript是如何序列化的參數給服務器。您可能會使用錯誤的請求MIME類型來編碼它們。 –

+0

是的,謝謝你的回覆親愛的約翰菲利普**。我爲我的問題找到了解決方案。 –

+0

不客氣。也許你可以發佈答案來幫助其他人遇到同樣的問題嗎? –

回答

0
 I found the solution to my question. 

There is no changes to my script. I simply call the parameter name in servlet page. The name is waypointlist. And I change my servlet code little bit. 

That is 

    String[] waypointlist=request.getParameterValues("waypointlist"); 

    String waypointarray=""; 

       for(int i=0;i<waypointlist.length;i++) 
       { 
        waypointarray += waypointlist[i] +"/"; 
       } 
+0

你可以做'String waypointarray = waypointlist.join('/');'那麼你不需要for循環。 – MotKohn

+0

ohhhh ...使用字符串函數是可能的。是的,謝謝你的好迴應 –