从servlet读取一个表单输入,其名称类似于input []

时间:2017-02-20 09:45:47

标签: javascript jquery jsp servlets

我想从JSP页面中的表单中读取一些输入,其中每个输入都具有相同的名称。见JSFiddle

如何从servlet中读取这些输入?

我尝试了String[] description = request.getParameterValues("description");,但它无法正常工作..

有没有办法循环并读取这些值?



counter = 0;
var dataList = $('.products');
var jsonOptions = [{
  "product": "11111",
  "description": "description 1"
}, {
  "product": "22222",
  "description": "description 2"
}, {
  "product": "33333",
  "description": "description 3"
}];

jsonOptions.forEach(function(item) {

  var option = '<option value="' + item.product + '">' + item.description + '</option>';

  dataList.append(option);
});

$(function() {
  $('body').on('input', '.product,.products', function() {

    var i = this.value;
    var description = "";
    var productsInBox = 0;

    jsonOptions.forEach(function(a) {
      if (a.product == i) {
        description = a.description;
        productsInBox = a.productsInBox;
      }
    });
    $(this).closest('.form-group').find('.description').val(description);


  });
});





$('#form1').on('click', '.addButtonDED', function() {
    var $template = $('.form-group:last').clone(true, true).find('input').val('').end().
    find('.addButtonDED').removeClass('addButtonDED').addClass('removeButtonDED').end()
      .find('i').removeClass('fa-plus').addClass('fa-minus').end();
    $template.insertAfter('.form-group:last');
  })

  // Remove button click handler
  .on('click', '.removeButtonDED', function() {
    var $row = $(this).closest('.form-group');
    $row.remove();
  });
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form1" method="post" class="form-horizontal" role="form">
  <fieldset>


    <div class="form-group">
      <div class="col-md-2">
        <input type="text" list="products" class="form-control product" name="product[]" />
        <datalist id="products" class="products"></datalist>
      </div>

      <div class="col-md-4">
        <input id="" type="text" class="form-control description" name=" description[]" />
      </div>



      <div class="col-md-1">
        <button type="button" class="btn btn-default addButtonDED"><i class="fa fa-plus"></i></button>
      </div>
    </div>

    <div class="col-md-10 ">
      <button type="submit" name="formAction" value="next" class="btn btn-primary">sUBMIT</button>

    </div>



  </fieldset>
</form>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

将名称更改为描述应该有效

<input type="text" class="form-control" name="description" />


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

我更喜欢第一个,因为它不那么令人困惑。

答案 1 :(得分:0)

在&lt; input&gt;中使用“description”作为名称,或使用request.getParameterValues("description[]"),但在两边使用相同的名称。

答案 2 :(得分:0)

这会奏效。

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