将数据从 javascript 传递到 servlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4496501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 13:53:40  来源:igfitidea点击:

passing data from javascript to servlet

javascripthtmljspservlets

提问by Sumithra

I have values of checkbox and as soon as I click delete the values and the type must be sent to a servlet called DeleteServlet. Please help me how to send the values?

我有复选框的值,一旦我单击删除值,必须将类型发送到名为 DeleteServlet 的 servlet。请帮我如何发送值?

<html>
    <head>
    <script type="text/javascript">
     function get_check_value(chkbox){
           if(chkbox.checked){
          var Values=chkbox.value;
          //send this Values to servlet

             }

    }
    function doDelete(theClick){
    var type=theClick;
//send this type to servlet
    alert(type);

    }

    </script>
    </head>
    <%String user=request.getParameter("Users"); %>
    <body>
    <%

    String User[]=user.trim().split(" ");

    for(int i=0;i<User.length;i++){%>
     <center>
        <form name='list' action='AddUserServlet'>

     <%  out.println("<input type='checkbox' name='user" + i + "' value='"+ User[i] + "' onClick='return get_check_value(this)'>" + User[i]+ "</input>" ); %>  

    </form> 

    <%}%>

        <input type="submit" value="ADD" name="b1" >
        <input type="submit" value="MODIFY" name="b1">
     <input type="submit" value="DELETE" name="b1" onClick="return doDelete(value)" > 

      </center>

    </body>
    </html>

I tried this "document.list.user.value" in an alert box it comes as undefined object. To send to a servlet I tried the following line but nothing is read

我在警告框中尝试了这个“document.list.user.value”,它作为未定义的对象出现。要发送到 servlet,我尝试了以下行,但没有读取任何内容

chk_val=chkbox.value;
document.add.action = "AddUserServlet?type=delete&name="+chk_val;
 document.add.submit();

回答by BalusC

You don't need Javascript for this at all.

您根本不需要 Javascript。

Just change your code so that the HTML form is generated as follows:

只需更改您的代码,以便按如下方式生成 HTML 表单:

<form action="AddUserServlet" method="post">
    <input type="checkbox" name="user" value="1">
    <input type="checkbox" name="user" value="2">
    <input type="checkbox" name="user" value="3">
    <input type="checkbox" name="user" value="4">
    <input type="checkbox" name="user" value="5">

    <input type="submit" name="b1" value="ADD">
    <input type="submit" name="b1" value="MODIFY">
    <input type="submit" name="b1" value="DELETE">
</form>

(the checkbox value must be the unique user ID)

(复选框值必须是唯一的用户 ID)

Then use request.getParameterValues()in your AddUserServlet:

然后request.getParameterValues()在您的AddUserServlet

String b1 = request.getParameter("b1");
if ("DELETE".equals(b1)) {
    String[] users = request.getParameterValues("user");
    // ...
}

The usersarray will contain the checked values (1, 2, 3, 4 and/or 5).

users数组将包含检查的值(1、2、3、4 和/或 5)。

回答by user257980

Could you use jQuery javascript libary. It is easy to use and you will find lots of tutorials. Here is good tutorial about get method(AJAX) http://api.jquery.com/jQuery.get/

你能用jQuery javascript libary吗?它易于使用,您会发现很多教程。这是关于获取方法(AJAX)http://api.jquery.com/jQuery.get/的很好的教程

Is it possible to do it without javascript?

没有javascript可以做到吗?