如何将<div>内容传递给servlet?</div>

时间:2014-05-22 10:13:51

标签: javascript jquery html servlets

我尝试使用JS传递表单操作URL中的整个div的内容。但是在servlet端,当我尝试将此请求参数作为String时,我将其作为[object Object]。

这是我的表格和JS

<div id="paraContent">
   Content in jsp
</div>
<form id="form1" action="/sendEmail.email.send.html" method="post">
    <input type="hidden" id="contentValue1" name="contentValue1"/>
   
    <input type="button" value="place order" onClick="formSubmit()"/>
</form>

<script type="text/javascript">
    var divContent = $('#paraContent');
    $("#contentValue").val(divContent);

    function formSubmit() {
        var form = document.getElementById("form1");
        var str = "/sendEmail.email.send.html?contentValue=" + $('#paraContent');
        form.action = "/sendEmail.email.send.html?contentValue=" + $('#paraContent');
        form.submit();
    }
</script>

3 个答案:

答案 0 :(得分:0)

尝试更改

$(paraContent)$("#paraContent");以及

$("contentValue").val(divContent);$("#contentValue").val(divContent);

答案 1 :(得分:0)

当表单被提交时,您可以将div的内容复制到div中。

<form id = "form1" action="/sendEmail.email.send.html" method="post">
    <input type="hidden" id="contentValue1" name="contentValue1"/>
    <c:Set var="contVal" value="ddd"/>
    <input type="button" value="place order" onClick="formSubmit()"/>
</form>

<script type="text/javascript">

    function formSubmit() {
        $("#contentValue1").val($("#paraContent").text());
        var form = document.getElementById("form1");
        var str = "/sendEmail.email.send.html?contentValue=" + $(paraContent);
        form.action = "/sendEmail.email.send.html?contentValue=" + $(paraContent);
        form.submit();
    }
</script>

答案 2 :(得分:0)

您可以使用ajax post将数据从div发送到您的servlet

<div id="paraContent">
       Content in jsp
</div>



<script>
    $(document).ready(function (){
        var data=$("#paraContent").text();
     $.ajax({ 
    url: "YOUR SERVLET", 
    type: "POST", 
    data: { "data": data}, 
    success: function(data) { 
    console.log(data); 
    } 
    });
        });
</script>

在你的servlet中

String datafromdiv=request.getParameter("data");

尝试使用按钮点击功能.....