gpt4 book ai didi

javascript - 在if语句下的html中使用javascript变量

转载 作者:行者123 更新时间:2023-11-28 07:27:13 27 4
gpt4 key购买 nike

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

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

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

我想在 if 语句中比较 numItemsproperties_count,如下所示:

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

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

最佳答案

你可以做相反的事情。您可以在 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>

关于javascript - 在if语句下的html中使用javascript变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29511167/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com