jsp传值学习——将同一jsp页面中的js的值转给java变量

表格和按钮

<tr>
            <td id="tds<%=i%>"><%=trainType%></td>
            <td><%=seat1%></td>
            <td><%=seat2%></td>
            <td><%=seat3%></td>
            <td id="tdss<%=i%>"><%=startStation%></td>
            <td id="tdsss<%=i%>"><%=endStation%></td>
            <td><input type="button" value="一等座购票" class="submit-button" style="font-size: 10px" onclick="buyFirst(<%=i%>)" id=""></td>
            <td><input type="button" value="二等座购票" class="submit-button" style="font-size: 10px"></td>
            <td><input type="button" value="三等座购票" class="submit-button" style="font-size: 10px"></td>
        </tr>

隐藏的表单

<%--用以传输js的值 火车票的有关信息--%>
<form action="http://localhost:8080/TrainTicketingSystem/buyTickets.jsp" style="display: none" id="budeyi">
    <input type="text" name="trainType" value="" id="trainType_js">
    <input type="text" name="start_station_name" value="" id="start_station_name_js">
    <input type="text" name="end_station_name" value="" id="end_station_name_js">
</form>

传值的js方法

<script>

    function check_before() {

        var  myselect=document.getElementById("start");
        var index=myselect.selectedIndex;
        var city1 = myselect.options[index].text;

        var  myselect2=document.getElementById("end");
        var index2=myselect2.selectedIndex;
        var city2 = myselect2.options[index2].text;

        if (index==index2){
            alert("始发站和终点站不能相同!请重新选择")
        }else {
            document.getElementById("startStation").value = city1;
            document.getElementById("endStation").value = city2;

            document.getElementById("submit_form").submit();
        }

    }

    function buyFirst(i) {

        var index = i;

        var trainType_js = document.getElementById("tds"+index).innerText;
        var start_station_name = document.getElementById("tdss"+index).innerText;
        var end_station_name = document.getElementById("tdsss"+index).innerText;

        document.getElementById("trainType_js").value = trainType_js;
        document.getElementById("start_station_name_js").value = start_station_name;
        document.getElementById("end_station_name_js").value = end_station_name;

        document.getElementById("budeyi").submit();

        <%
        if (namePeople!=null&&!namePeople.equals("null")){

            String trainType = request.getParameter("trainType");
            String start_station_name = request.getParameter("start_station_name");
            String end_station_name = request.getParameter("end_station_name");

System.out.println("********************"+trainType+start_station_name+end_station_name);//测试代码

            %>




        <%
        }
        else {
            %>
        alert("请先登录!!!");
        <%
        }
        %>
    }
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值