簡體   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