从 Jquery 库获取默认值而不是用户输入的值

Getting default value when from Jquery library not user entered value

在我的项目中,我们使用 spring、JSTL、bootstrap 和 jquery。 在 spring 控制器的后端,我将食物值列表分配给键 "foodList"。在 UI 中,我对其进行迭代并显示值。

我将字段 "weight" 作为文本输入。默认值将打印从后端设置的内容。如果用户想要更改,他可以这样做。

当用户更改值时,我想获得修改后的值,但在 jquery 代码中,我总是获得默认值,而不是用户更新的值。

有人可以帮助我的代码中有什么问题吗?

HTML代码,

<table id="table_food"
    class="table table-bordered">
    <thead>
        <tr>
            <th>Quantity</th>
            <th>Food Name</th>
        </tr>
    </thead>

    <tbody>
        <c:forEach items="${foodList}" var="det">
            <tr class="clickable-row" id="${det.id}">
                <td><input type="text" class="inputWeight"
                    value="${det.weight}" id="abcd" /></td>
                <td><div data-toggle="tooltip" data-placement="top"
                        title="${det.desc}" class="trunc">
                        <c:out value="${det.desc}" />
                    </div></td>
        </c:forEach>
    </tbody>
</table>

Jquery代码,

$(document).ready(function(){ 
$(".inputWeight").change(function(){
    alert($(this).val()); }); 
});

您的 jQuery code.the 没有得到所需输出的原因是您实际上将输入值与变量绑定只是做一件事将先前的数据放入占位符值并设置值属性空白