【问题标题】:use javascript variable in html under if statement在 if 语句下在 html 中使用 javascript 变量
【发布时间】:2015-06-13 04:56:00
【问题描述】:

我有一个 html 页面,我在其中动态显示属性列表。接下来,我统计每个 div 中显示的属性数量。

  <script type="text/javascript">
        var numItems = $('.countdiv').length;
        </script>

countdiv 是每个属性的类名。现在在我的 html 页面中,我有一个变量 properties_count,其中包含从我的数据库中获取的总属性的计数。

我想在if 语句中比较numItems 和properties_count,如下所示:

{% if(numItems!=properties_count) %}
     # some function to load more properties

但是比较不起作用。如何在 html 中使用 numItems(javascript 变量)?

【问题讨论】:

  • 那是因为 numItems 是一个 javascript 变量
  • 是的,我知道,这就是为什么我在标题中提到,如何在 html 中使用 javascript 变量。你能解释一下如何在 html 中使用 numItems 变量吗?
  • Jquery 模板可以帮助你。
  • Jinja 在 javascript(在浏览器中)之前执行(在服务器上),所以你想要的无法完成。你应该使用 Yuv 的方法。

标签: javascript jquery python html jinja2


【解决方案1】:

你可以反过来。您可以在 javascript 中使用 django/jinja2 变量

<script type="text/javascript">
    var numItems = $('.countdiv').length;
    var properties_count = '{{properties_count}}';
    if (numItems != properties_count) {
        //Do you code using javascript 
    }
</script>

【讨论】:

  • 这并不能解决我的问题朋友,我只想在 html 中使用 javascript 变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
  • 2018-05-09
  • 1970-01-01
相关资源
最近更新 更多