在不使用URL的情况下获取下一页上具有相同名称的输入类型值

时间:2013-12-02 07:25:03

标签: javascript jquery html jsp

使用以下代码在jsp页面(one.jsp)中使用JQUERY动态添加文本框:
How To Add Textbox Dynamically With JQuery

比如说,用户通过点击“添加文本框”添加3个文本框,输入这些值后,用户点击提交。
提交后我想在下一页(nextpage.jsp)上的多个输入类型文本框名称myarray []下访问这3个值。
实际上我想在下一页(jsp页面)上访问这些值。

尝试使用request.getparameter,但它导致检索第一个值,即第一个索引:
取名为“myarray []”并使用
获取          <%= request.getParameter("myarray[]")%> : my array values开启 下一页`

我现在应该做什么,是否可以选择这样做或在此处进行一些更改:
这是第one.jsp页中的表单:

<form action="nextpage.jsp" method="get" enctype="multipart/form-data">
<b>Add value in separate textboxes:</b>
     <div id='TextBoxesGroup'>
        <div id="TextBoxDiv1">
         <label>newvalue #1 : </label><input type='textbox' id='textbox1' name="myarray[]">
        </div>
     </div>
     <input type='button' value='Add textbox' id='addButton'><br />

   <input type="submit" value="Click to get result on next page"/><br /><br />  

以下是代码段:

<html>
    <head>

        <title></title>
<script type="text/javascript" src="jquery-2.0.3.min.js"></script>
<style type="text/css">

 div{
        padding:8px;
    }



 </style>

 </head>
<body>

    <h2>Code for adding multiple textboxes dynamically... <br /></h2> 


     <br />

<script type="text/javascript">
                   $(document).ready(function(){

    var counter = 2;

    $("#addButton").click(function () {

    if(counter>10){
            alert("Only 10 textboxes allow");
            return false;
    }   

    var newTextBoxDiv = $(document.createElement('div'))
         .attr("id", 'TextBoxDiv' + counter);

    newTextBoxDiv.after().html('<label>newvalue #'+ counter + ' : </label>' +
          '<input type="text" name="textbox' + counter + 
          '" id="textbox' + counter + '" value="" >');

    newTextBoxDiv.appendTo("#TextBoxesGroup");


    counter++;
     });
        });          
  </script>

注意:忽略计数器值,因为它只是用于检查目的,代码运行正常但它只使用request.getparameter检索第一个索引值。
请建议我,JQUERY和Javascript的新手

2 个答案:

答案 0 :(得分:2)

您必须在输入的名称属性中添加相同的名称,为此您必须编辑您的行

newTextBoxDiv.after().html('<label>newvalue #'+ counter + ' : </label>' +
      '<input type="text" name="textbox' + counter + 
      '" id="textbox' + counter + '" value="" >');

对此:

newTextBoxDiv.after().html('<label>newvalue #'+ counter + ' : </label>' +
      '<input type="text" name="myarray[]" id="textbox' + counter + '" value="" >');

在yout jsp页面中,您可以:&lt;%= request.getParameterValues(“myarray []”)%&gt;

请记住:您不必将“[]”放在jsp或除php之外的任何其他语言

答案 1 :(得分:1)

  

代码正在正常运行,但它只使用request.getparameter检索第一个索引值。

使用getParameterValues方法

String[] valueArray = request.getParameterValues("myarray[]");  

返回包含给定请求参数myarray[]具有的所有值的String对象数组,如果参数不存在,则返回null