使表行可单击并将post参数传递给jquery函数

时间:2014-06-15 21:09:23

标签: java javascript jquery jstl el

我创建了一个表,并用通过JSTL标记和EL表​​达式获取的元素填充它,如

<c:forEach items="${routes_list}" var="route">
    <tr>
       <td>${route.airlane}</td>
       <td>${route.aircraft_id}</td>
       <td>${route.airport_city_source.city}</td>
       <td>${route.airport_city_dest.city}</td>
       <td><fmt:formatDate value="${route.departure_date}" pattern="dd/MM/yyyy hh:mm"/></td>
       <td><fmt:formatDate value="${route.arrival_date}" pattern="dd/MM/yyyy hh:mm"/></td>
       <td>${route.travel_class}</td>
     </tr>
 </c:forEach>

我想做的是让每一行都可以点击。我通过使用此功能

实现了这一目标
$("tr").click(function() {
window.location.href = $(this).find("a").attr("href");
});

但我的真正目标是传递给href ${route.id},以便向servlet发送一个post请求并完成一些任务。我不是一个jquery程序员,所以我不知道如何继续。谢谢大家

修改

$(".show_reservations").click(function() { //show.reservations css class
var href  = $(this).find("a").attr("href");
$.ajax({
    cache:false,
    dataType:"html",
    data:"${route.id}="+href,
    type: "POST",
    url: "admin_show_reservations", //NAME OF THE SERVLET
    success: function(data)
             {
                //console.log(data);
                alert("success"+data);
             },
    error: function(exception)
    {
    alert("error");
    }
    });
});

HTML

<section class="area_principal">
    <h4>Voli</h4>
    <div class="body_area_principal">
        <c:choose>
            <c:when test="${!empty routes_list}">
                <table class="show_reservations">
                    <tr>
                        <th>Compagnia</th>
                        <th>ID Veivolo</th>
                        <th>Partenza</th>
                        <th>Arrivo</th>
                        <th>Ora partenza (ora locale)</th>
                        <th>Ora arrivo (ora locale)</th>
                        <th>Classe</th>
                    </tr>
                    <c:forEach items="${routes_list}" var="route">
                        <tr>
                            <td>${route.airlane}</td>
                            <td>${route.aircraft_id}</td>
                            <td>${route.airport_city_source.city}</td>
                            <td>${route.airport_city_dest.city}</td>
                            <td><fmt:formatDate value="${route.departure_date}" pattern="dd/MM/yyyy hh:mm"/></td>
                            <td><fmt:formatDate value="${route.arrival_date}" pattern="dd/MM/yyyy hh:mm"/></td>
                            <td>${route.travel_class}</td>
                        </tr>
                    </c:forEach>
                </table>
            </c:when>
            <c:otherwise>
                <p>Non ci sono voli nel sistema. Aggiungerli attraverso la voce presente nel menù sulla sinistra</p>
            </c:otherwise>
        </c:choose>
    </div>
</section>

生成的HTML

<section class="area_principal">
    <h4>Voli</h4>
    <div class="body_area_principal">


                <table class="show_reservations">
                    <tr>
                        <th>Compagnia</th>
                        <th>ID Veivolo</th>
                        <th>Partenza</th>
                        <th>Arrivo</th>
                        <th>Ora partenza (ora locale)</th>
                        <th>Ora arrivo (ora locale)</th>
                        <th>Classe</th>
                    </tr>

                        <tr>
                            <td>Alitalia</td>
                            <td>AZ198EG</td>
                            <td>Catania</td>
                            <td>Milan</td>
                            <td>18/06/2014 07:00</td>
                            <td>18/06/2014 08:35</td>
                            <td>Economy</td>
                        </tr>

                        <tr>
                            <td>Emirates</td>
                            <td>EA343E3</td>
                            <td>Catania</td>
                            <td>Dubai</td>
                            <td>18/06/2014 09:00</td>
                            <td>19/06/2014 05:00</td>
                            <td>Economy</td>
                        </tr>

                </table>



    </div>
</section>

1 个答案:

答案 0 :(得分:1)

$("tr").click(function() {
var href  = $(this).find("a").attr("href");
 $.ajax({
                cache:false,
                dataType:"html",
                data:"yourparameter="+href,
                type: "POST",
                url: "yourServletContextPath",
                success: function(data)
                    {
                        //console.log(data);
                        alert("success"+data);
                    },
                error: function(exception)
                    {
                    alert("error");
                    }
                });
});

&#34;数据&#34;将包含您的html类型响应