gpt4 book ai didi

java - 如何定位具有使用 Thymeleaf 组成的 id 的输入字段的值

转载 作者:行者123 更新时间:2023-12-02 09:36:01 24 4
gpt4 key购买 nike

单击按钮时我想获取输入字段的值。但是,我在定位输入字段时遇到问题,因为字段 id 是另一个组合值。

我尝试过 this.value ,它似乎只向我传递 req.id,而不是在输入字段中输入的值。我知道获取输入字段值的正确方法是:$(选择器).val()

但是,以下内容不适用于当前的实现。$(incidentNumber+${req.id}).val()

<tr th:each="req : ${requests}">       
<td><input th:id="incidentNumber+${req.id}" th:name="incidentNumber+${req.id}"
style="width:100%" type="text" th:value="${req.incidentNumber}">
<button th:value="${req.id}"class="button" type="button" onclick="validate_ticket_number(this.value)">Update</button></a>
</td>
</tr>

我希望输出是“incidentNumber+${req.id}”输入字段中输入的值

最佳答案

连接字符串的正确方法是:

th:id="'incidentNumber'+${req.id}"

当您在 Javascript 中访问它时(通过元素的 ID 进行 jQuery 选择器),您不能使用 thyemeleaf 表达式,它必须是:

$('#incidentNumber100').val() // req id 100 is just an example

如果要使其动态化,请将元素的 Id 传递给目标函数,然后在函数中使用 Id 检索值。

<button th:value="${req.id}"class="button" type="button"
th:onclick="'validate_ticket_number(\'incidentNumber'+${req.id}+'\')'"> Update</button>

然后在 validate_ticket_number() 函数中使用提供的 ID 访问该值,如下所示:

function validate_ticket_number(id){
var value = $(id).val();
}

关于java - 如何定位具有使用 Thymeleaf 组成的 id 的输入字段的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57514732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com