简体   繁体   English

使用Django模板在javascript函数中传递变量

[英]passing variable in javascript function using django template

here I am passing {{id}} to hide_show(...) javascript function 这里我将{{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 }}

here above the {% endwith %} {{ id }} is displaying correctly but the hide_show function in not called but it is called when just {{ stock.id }} is passed to it. {%endwith%} {{id}}上方的此处显示正确,但是未调用hide_show函数,但仅将{{stock.id}}传递给了hide_show函数。 the concatenate filter just concatenates and returns a string. 串联过滤器只是串联并返回一个字符串。

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

Problem seems to be with the quotes. 问题似乎出在引号上。 if {{ id }} returns a string, then you don't have to put quotes here 如果{{id}}返回一个字符串,那么您不必在这里加引号

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

Simply replace it with: 只需将其替换为:

<div id={{ id }}>

If id is an integer or if the above doesn't work, then you have to put quotes here as well as in the argument while calling the hide_show function. 如果id是整数或以上方法不起作用,则必须在调用hide_show函数时在引号和引数中添加引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM