admin管理员组文章数量:1410737
I'm struggling a bit with joining jstl and jquery.
I have a Spring aplication which provides me with a list, for example cars:
[Kia, Audi, Fiat, Mazda, Opel]
I have an input field that if the user presses the button next to it, it adds this text in form of fancy tag, or whatever. Furthermore I want to add all list elements in same form of this tag while the page loads. But if I try I receive :
Uncaught ReferenceError: add_tag is not defined (anonymous function)
So I have code as follows (of course strip down to very problem)
<script>
$(document).ready(function() {
function add_tag(value) {
console.log("adding tag:" + value);
//some styling
$("#add_here").append(value);
};
$("#add_tag").click(function() {
add_tag($("#choosen_tag").val());
});
});
</script>
<div>
<c:forEach items="${cars}" var="c">
<script>
add_tag("${c}");
</script>
</c:forEach>
</div>
<input id="choosen_tag"></input>
<button type="button" class="btn btn-default" id="add_tag">+</button>
<div id="add_here"></div>
All jquery, jstl tags etc, are of course included. The sole issue here is calling this function. Tried something with named functions, or declaring it within some other var, still no success :/
I'm struggling a bit with joining jstl and jquery.
I have a Spring aplication which provides me with a list, for example cars:
[Kia, Audi, Fiat, Mazda, Opel]
I have an input field that if the user presses the button next to it, it adds this text in form of fancy tag, or whatever. Furthermore I want to add all list elements in same form of this tag while the page loads. But if I try I receive :
Uncaught ReferenceError: add_tag is not defined (anonymous function)
So I have code as follows (of course strip down to very problem)
<script>
$(document).ready(function() {
function add_tag(value) {
console.log("adding tag:" + value);
//some styling
$("#add_here").append(value);
};
$("#add_tag").click(function() {
add_tag($("#choosen_tag").val());
});
});
</script>
<div>
<c:forEach items="${cars}" var="c">
<script>
add_tag("${c}");
</script>
</c:forEach>
</div>
<input id="choosen_tag"></input>
<button type="button" class="btn btn-default" id="add_tag">+</button>
<div id="add_here"></div>
All jquery, jstl tags etc, are of course included. The sole issue here is calling this function. Tried something with named functions, or declaring it within some other var, still no success :/
Share Improve this question edited Oct 6, 2015 at 14:46 James 22.4k5 gold badges29 silver badges43 bronze badges asked Mar 24, 2014 at 22:01 KhobarKhobar 4767 silver badges21 bronze badges 01 Answer
Reset to default 5Minutes after posting, I found a solution. Script should use jstl instead of the other way around. Adding
<script> $(document).ready(function() {
<c:forEach items="${cars}" var="c">
add_tag("${c}");
</c:forEach>
// rest of script adding functions
...
solves my issue.
版权声明:本文标题:javascript - Calling jQuery function from jstl c:forEach ( function not defined) on page load - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744858023a2628893.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论