【问题标题】:passing variable in javascript function using django template使用 django 模板在 javascript 函数中传递变量
【发布时间】:2017-07-06 14:22:56
【问题描述】:

这里我将 {{id}} 传递给 hide_show(...) javascript 函数

{% for stock in part_temp.part_stock_set.all %}
    {% with id="list"|concatenate:stock.id %}
    <div id="{{ id }}">
    {{ stock.entry_date}}
    </div>
    <button type="button" onclick="hide_show({{ id }})">edit</button>
    <br>
        {{ id }}

在 {% endwith %} {{ id }} 上方显示正确,但未调用 hide_show 函数,但仅在 {{ stock.id }} 传递给它时调用它。 连接过滤器只是连接并返回一个字符串。

    <script type="text/javascript">
    function hide_show(temp) {
        document.getElementById(temp).style.display='none';
        window.alert(temp);
    }
</script>

【问题讨论】:

    标签: javascript django django-templates


    【解决方案1】:

    问题似乎与引号有关。如果 {{ id }} 返回一个字符串,那么您不必在此处加上引号

    <div id="{{ id }}">
    

    只需将其替换为:

    <div id={{ id }}>
    

    如果 id 是一个整数或者上面的方法不起作用,那么你必须在调用 hide_show 函数时在此处和参数中加上引号。

    【讨论】:

    • 它不是一个整数,虽然在调用 javascript 函数时参数中的单引号有效
    猜你喜欢
    • 2020-10-20
    • 1970-01-01
    • 2011-05-09
    • 2018-12-03
    • 2020-10-23
    • 2013-12-16
    • 2018-12-20
    • 1970-01-01
    • 2020-09-10
    相关资源
    最近更新 更多