form表单提交带参数的两种方式

#第一种方式#

action写明了LoginServlet,通过submit按钮直接提交到后台

<form action="LoginServlet" method="post">
    <input type="text" id="inputUsername" name="username" placeholder="Username" required autofocus>
    <input type="password" id="inputPassword" name="password" placeholder="Password" required>
    <button type="submit">登 录</button>
</form>

后台LoginServlet接收数据,注意前台注明了是method=“post”,所以要在doPost()方法下写

String username = request.getParameter("username");
String password = request.getParameter("password");

#第二种方式#

不希望直接提交,而是通过按钮点击手动提交,适用于需要传入form之外的其他动态参数的时候

这里写了一个表单exportForm,为了美观,除了下载按钮,另外两个input设置成了不可见状态,即隐藏状态

<form id="exportForm" class="sel_btn" action="ExportExcelServlet" method="post" style="display: none">
     <input type="text" id="input_start" name="startdate" style="display:none">
     <input type="text" id="input_end" name="enddate" style="display:none">
     <input type="button" class="sel_btn" value="下载清单" onclick="ExportData()">
</form>

在ExportData()里面再通过submit()提交 

function ExportData(){
    //传参到form表单隐藏的input标签里面去
    $("#input_start").val(start);
    $("#input_end").val(end);

    //form表单提交
    document.getElementById("exportForm").submit();
}

后台ExportExcelServlet获取参数

String StartDate = request.getParameter("startdate");
String EndDate = request.getParameter("enddate");

#tips#

有以下两个控件,分别是获取起始日期和终止日期的输入框

<input type="text" id="startDate" name="startDate">
<input type="text" id="endDate" name="endDate">

以下获取控件值并对另一个控件进行赋值的两种方式完全等价

document.getElementById("input_start").value = document.getElementById("startDate").value;
document.getElementById("input_end").value = document.getElementById("endDate").value;

$("#input_start").val(document.getElementById("startDate").value);
$("#input_end").val(document.getElementById("endDate").value);

 区别仅仅在于$.val()是JQuery的取值方式

document.getElementById("input_start").value;
$("#input_start").val()

友情链接:

一个form表单对应多个submit  https://www.cnblogs.com/cyfblogs/p/9851440.html

  • 2
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值