我有一個將值傳遞給我的js文件的問題。 我試過這樣做根據http://feather.elektrum.org/book/src.html和其他職位,但它不工作。從html傳遞值到js文件
我的HTML文件
<script type="text/javascript">
var user = 'example_of_user_name';
</script>
<script type="text/javascript" th:src="@{/js/file.js}"></script>
,並在我的file.js我願做這樣的事情
content[0] = '<div class="thumbnail">\
<form style="display:inline" method="POST" action="/..." class="from-horizontal">\
<input type="hidden" id="player" value=user name="player" />\
<button class="btn btn-info" type="submit">Submit</button>\
</form>\
</div>'
但在我輸入該值始終爲空。當我在我的輸入中靜態分配值時,一切正常。
<input type="hidden" id="player" value='example_of_user_name' name="player" />\
編輯:
我的命名空間
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring3-3.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
後評論我也試過
content[0] = '<div class="thumbnail">\
<form style="display:inline" method="POST" action="/..." class="from-horizontal">\
<input type="hidden" id="player" value=' + user + ' name="player" />\
<button class="btn btn-info" type="submit">Submit</button>\
</form>\
</div>';
或
document.getElementById('player').value = user;
但它不起作用。我仍然得到空指針。
'th:src =「@ {/ js/file.js}」'不是有效的html語法。 –
爲什麼不呢?我忘了提及我也使用了thymeleaf – Armyx
你已經給出了_src_ _th的_namespace_,但沒有向我們顯示任何暗示你有'th' _namespace_定義的東西。您還在屬性_ @ {...} _中包含_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ –