如何将参数名称作为数组?

时间:2017-07-19 09:22:48

标签: javascript arrays

在我的代码中我正在使用谷歌地图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);

1 个答案:

答案 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] +"/";
                }