简体   繁体   中英

how to get a value from a dynamic span

i would get the value of a dynamic form from a jsp to a servlet. the code in the jsp is the following

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;
 }

in the servlet, i would get the value of "rata"+i.
how i can do this??

Thanks!

In your doGet or doPost method based on form type in servlet you can iterate all parameters and check if parameter starts with rata then its accepted

to get all parametes names use request.getParameterNames() which will return 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
    }
}

You will have to wrap your textbox in a form tag.

// 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>";

So when you click the submit button, the form will get submitted by all the values in your all textboxes.

MyServlet

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
    }


 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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