單擊按鈕我想獲取輸入字段的值。但是,我無法定位輸入字段,因為字段 id 是另一個組合值。我試過this.value這似乎只傳遞了 req.id 而不是輸入字段中輸入的值。我知道獲取輸入字段值的正確方法是: $(selector).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}”輸入字段中輸入的值
1 回答

慕桂英3389331
TA貢獻2036條經驗 獲得超8個贊
連接字符串的正確方法是:
th:id="'incidentNumber'+${req.id}"
當您在 Javascript 中訪問它時(jQuery
通過元素的 Id 選擇器)您不能使用表達式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(); }
添加回答
舉報
0/150
提交
取消