在jsp页面中获取表格中的文本框里的数据和与其同一行的第一格中的数据的一个方法

直接上代码

表格代码

<c:forEach var="food" items="${requestScope.menu.list}">
    <tr>
        <td>${food.id}</td>
        <td>${food.foodName}</td>
        <td>${food.foodPrice}</td>
        <td class="buy"><input type="text" class="inputs" placeholder="请输入添加数量"></td>
    </tr>
</c:forEach>

用jstl中的foreach方法动态生成表格,因为用了PageHelper的缘故,所以items中的参数会写成那样。

jQuery代码

 $(".inputs").blur(function () {
        const count = $(this).val();
        const foodId = $(this).closest("tr").find("td").first().text();
        console.log("count:" + count);
        console.log("foodId:" + foodId);
        $.ajax({
            url: "http://localhost:8081/SSM_war_exploded/ordermanage",
            method:"POST",
            data:{foodId:foodId,count:count},
            contentType:"application/x-www-form-urlencoded; charset=utf-8",
            error:function (res) {
                alert("添加失败!");
            }
        })
    })

其中$(".inputs")是选择器,选中了该文本框,.blur是该对象失去焦点时执行,$(this)指该对象,.closest("tr")指寻找到离该文本框最近的<tr>标签对象,.find("td").first()指在该<tr>标签对象下寻找第一个<td>标签对象,.test()指获取其中内容。

这样就可以获取这些内容然后用ajax传递给后端了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值