2017-07-19 63 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 ...使用字符串函数是可能的。是的,谢谢你的好回应 –