在页面加载时从jstl c:forEach(函数未定义)调用jQuery函数 [英] Calling jQuery function from jstl c:forEach ( function not defined) on page load

查看:219
本文介绍了在页面加载时从jstl c:forEach(函数未定义)调用jQuery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在加入jstl和jquery方面有点挣扎。
我有一个Spring应用程序,它为我提供了一个列表,例如汽车:
[起亚,奥迪,菲亚特,马自达,欧宝]

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 :


未捕获的ReferenceError:未定义add_tag(匿名函数)

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>

当然包括所有jquery,jstl标签等。这里唯一的问题是调用此函数。尝试使用命名函数或在其他var中声明它仍然没有成功:/

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 :/

推荐答案

发布后几分钟,我发现一个办法。脚本应该使用jstl而不是相反。添加

Minutes 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
...

解决了我的问题。

这篇关于在页面加载时从jstl c:forEach(函数未定义)调用jQuery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆