English 中文(简体)
从一个共同财产调查组到另一个实体通过一些检查箱的数值
原标题:Pass some checkbox values from one JSP to another

I ve been having problems passing parameters from one .jsp to another.

I have two .jsp (1 and 2). In 1 I get some data from a database and show the user a bunch of checkboxes (depending on the data I got before). The user has to check one or more of the checkboxes, the selected will be deleted in my database in 2. (It´s something like "Select the numbers you want to delete").

I don t know how to pass the selected checkboxes and the value from 1 to 2. I tried with javascript/jQuery, trying to know if a checkbox is checked and its value, add the value to a hidden field and use the request in 2 to get it.

1.jsp

<%
    HttpSession sesion = request.getSession();

    Company company = (Company) sesion.getAttribute("company");
    List<Phone> phones = company.getTelefonos();
%>
<form id="formulario" method="POST" action="desMul_Final.jsp">
<fieldset>
    <legend>Numbers</legend>
    <%
        Iterator<Phone> it1 = phones.iterator();
        while(it1.hasNext()){
            Phone t = it1.next();
            String number = t.getNumero();
    %>
    <p>
        <input name=check id="t_<%=number%>" type=checkbox value="<%=number%>" /> <%=number%>.
    </p>
    <%
        }
    %>
</fieldset>
<p class="buttons">
    <button type=submit onclick="javascript: pick();">Continue</button>
</p>
</form>

Javascript/jQuery

function pick(){
var counter = 0;
$("#formulario fieldset p").each(function(index){
    var field;
    $(this).children("input").each(function() {
        if($this.is( :checked )){
            field = $(this).val();
        }
    });
    index = index + 1;

    texto = "<input type=hidden name=phone_"+index+" value="+field+" />";
    $("#formulario").append(texto);

    counter = index;
});
cant = "<input type=hidden name=amount id=amount value="+counter+" />";
$("#formulario").append(cant);
}

2.jsp(我只想知道我是否掌握了信息)

<%
    int amount = Integer.valueOf(request.getParameter("amount"));
    System.out.println(amount);

    for(int i = 1; i <= amount; i++){
            String s = request.getParameter("phone_"+i);
        System.out.println(s);
    }
%>

当我试图查阅<代码>request.getPara amount(“amount”)时,我收到一份<代码>java.lang.NumberFormatException:un,因此我认为我的 Javascript/jQuery是错误的。

我如何解决这一问题?

最佳回答

我最后找到了解决办法,我确实需要任何 Java字。 在APICI中搜索了请求中的一种方法,即:getParaifNames(>,returns an E数 of String Object contained in this request.

我的理解是,当你提交表格时,如果对控制进行核对,就会寄出检查箱的价值。 否则,将无所作为。 因此,由于有了这种信息和方法,我改变了检查箱的建造方式,并用计数来获取这些数值。

BEFORE  
<input name=check id="t_<%=number%>" type=checkbox value="<%=number%>" /> <%=number%>.

NOW  
<input name="<%=number%>" type=checkbox /> <%=number%>.

So in my second .jsp I get a Enumeration with the names of the selected checkboxes, which are the phone numbers.

java.util.Enumeration enu = request.getParameterNames();
java.util.List<String> numbers = new java.util.ArrayList<String>();
while(enu.hasMoreElements()){
   String s = (String)(enu.nextElement());
   System.out.println(s);
   numbers.add(s);
}
问题回答

Are you familiar with debugging web application in a client-side way? Can you please insert console.log("$("#formulario")) and then a breakpoint after $("#formulario").append(cant);, to see how the form s content changed, and if the hidden input was added ?

使用<代码>int[] amount= Integer.ValueOf(request.getParaileValues(“amount”);,而不是request.getPara amount(“amount”);





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签