繁体   English   中英

如何从动态范围中获取值

[英]how to get a value from a dynamic span

我会从jsp到servlet获得动态表单的值。 jsp中的代码如下

function AggiungiRiga(n_righe){


var numero_righe = n_righe.value;
var box = document.getElementById('box_righe');
if(isNaN(numero_righe)==true){
    box.innerHTML='';
}else{
    var righe = "";
    // Inserisco una riga ad ogni ciclo
    for( i=1; i<=numero_righe; i++){
        righe = righe+""+i+") Ricercatore  : <input type='text' name='rata"+i+" size='10'  maxlength='10'/><br/>";
        String s=rata1.florinda();
        out.println(s);
        }box.innerHTML=righe;

}
  return numero_righe;
 }

在servlet中,我将获得“ rata” + i的值。
我该怎么做?

谢谢!

在基于servlet中表单类型的doGet或doPost方法中,您可以迭代所有参数,并检查参数是否以rata开头,然后接受

要获取所有参数名称,请使用request.getParameterNames() ,该方法将返回Enumeration。

Enumeration<String> params = request.getParameterNames();
while (params.hasMoreElements()) {
    String param = params.nextElement();
    if(param.startsWith("rata")){
         String value = request.getParameter(param); 
         //your code here
    }
}

您将不得不将您的文本框包装在一个表单标签中。

// Inserisco una riga ad ogni ciclo
for( i=1; i<=numero_righe; i++){
    righe = righe+""+i+") Ricercatore  : <input type='text' 
                           name='rata"+i+" size='10'  maxlength='10'/><br/>";
    String s=rata1.florinda();
    out.println(s);
    }
    //Adding form tag  and submit button
    //Aggiunta di tag forma e pulsante di invio
    box.innerHTML="<form method=post action=myservlet >"
                     +righe
                     +"<input type=submit name=submit value=Submit />"
                     +"</form>";

因此,当您单击“提交”按钮时,将通过所有文本框中的所有值来提交表单。

我的Servlet

protected void doPost(HttpServletRequest request,HttpServletResponse response)
 throws ServletException,IOException{
    Enumeration<String> rataList = request.getParameterNames();

   while(rataList.hasMoreElements()){
    request.getParameter(rataList.nextElement()); 
    //Do what you want here 
    //Fate quello che volete qui
    }


 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM