javascript - python Django : How can I determine if a div should show or hide based on the parameter passed to template

标签 javascript jquery django

我需要根据从 djangoviews.py 传入的参数设置一个 div 来显示或隐藏,但我就是无法让它工作。我的代码如下:

在正文中:

{% if report_type == "detail" %}
    visibility("detail");
{% endif %}

在 JavaScript 中:

<script type="text/javascript">
    function visibility(reportType) {
        console.log(reportType);
        if(reportType == "detail") {
            $('#executive_summary').style.display = 'none';
        }
    }
</script>

但是上面的代码不起作用。有人可以给我一些建议吗?谢谢。

最佳答案

通过纯CSS:

        {% if report_type != "detail" %}
            <div style="display: none;">
        {% else %}
            <div style="display: block;">
        {% endif %}
        ...
        </div>

或者使用 JavaScript (jQuery):

    {% if report_type != "detail" %}
    <script type="text/javascript">
        $('#executive_summary').hide();
    </script>
    {% endif %}

通过 JavaScript 函数:

    <script type="text/javascript">
        /* If wanted to run when page is loaded. */
        $(function() {
            // Handler for .ready() called. 
            visibility("detail");
        });

        function visibility(reportType) {
            if(reportType == "detail") {
                $('#executive_summary').show();
            }
            else {
                $('#executive_summary').hide();
            }
        }
    </script>

关于javascript - python Django : How can I determine if a div should show or hide based on the parameter passed to template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10212326/

相关文章:

javascript - 生成 PDF 和 Excel 工作表,但在 Excel 中它既不打开也不选择保存....(就像在 Laravel 中一样)

javascript - Angularjs:如何将特定数据传递给模态

JavaScript 振荡

javascript - 安卓。网页 View 。使用 Javascript 显示页面的一部分

Javascript + 修改字符串

php - CakePHP AJAX 布局

javascript - jQuery 按钮 ui-btn-右错位置

python - 造轮时的绝对路径错误

django - Django 中的 M2M 关系验证

javascript - 获取 Django 站点中的 Disqus 中的评论是否为父评论