[英]Pass an attribute to an external javascript file in thymeleaf- passed as empty string
從我的控制器中,我將一個對象添加到我的模型中,我可以在 Html 中訪問它。 例子
redir.addFlashAttribute("userId",userId);
return modelAndView;
在 HTML 中,如果我訪問 userId
<input type="hidden" id="userId" th:value="${userId}" /> -
我可以看到結果。 我有一個外部 JS 文件,它有一個將 userId 作為輸入參數的函數。 如何將 userId 直接讀取到 JS 文件中? 因為如果我這樣做:
var userId=$("userId").val();
userId 不接受該值。 它顯示一個空字符串。 我該如何解決 ?
您必須在 html #userId
為 id 使用選擇器
var userId=$("#userId").val();
jQuery 的val
方法查看value
屬性,因此它忽略th:value
。 您需要使用.attr
方法,例如$('#userId).attr('th-value')
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.