Django中{%for%}需要注意的细节

题目

Django中for循环需要注意的细节

问题

  • 在{%for%}循环中添加onclick()函数往往会指向最后一个item,而不是指向其相对应的item

test_base.html

<!--
 * @Description: test_base.html
 * @Autor: 365JHWZGo
 * @Date: 2022-02-15 18:12:33
 * @LastEditors: 365JHWZGo
 * @LastEditTime: 2022-03-09 11:38:38
-->

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试页面</title>
</head>

<body>
    {% block content %} {% endblock content %}
</body>

</html>

test.html

<!--
 * @Description: 测试for循环模版
 * @Autor: 365JHWZGo
 * @Date: 2022-02-15 17:01:24
 * @LastEditors: 365JHWZGo
 * @LastEditTime: 2022-03-09 19:43:28
-->
{% extends "test_base.html" %} {% block content %}
<table class="content">
    <th>
        <td>用户id</td>
        <td>姓名</td>
        <td>性别</td>
        <td>出生日期</td>
    </th>
    {% for item in user %}
    <tr>
        <td>{{item.id}}
            <a onclick="getId()" style="background-color: orange; cursor: pointer;">点击我获取id</a>
        </td>
        <td>{{item.username}}</td>
        <td>{{item.sex}}</td>
        <td>{{item.birthday}}</td>
    </tr>
    <script>
        console.log('item.id=', '{{item.id}}');

        function getId() {
            console.log('func getId->item.id=', '{{ item.id}}');
        }
    </script>
    {% endfor %}
</table>
<script src="https://cdn.bootcss.com/jquery/2.1.2/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
{% endblock content %}

在这里插入图片描述
此时在控制台输出的是
在这里插入图片描述
当点击第一项是即id=1,此时控制台的输出为
在这里插入图片描述
此时你会发现无论点击哪一个都会显示item.id=6

这是因为script总是在css、html加载完成后才会调用,这样导致在for循环中的getId()函数无法判别到底是调用的是哪一个。

为了进一步发现问题,我们打开
在这里插入图片描述


    <tr>
        <td>1
            <a onclick="getId()" style="background-color: orange; cursor: pointer;">点击我获取id</a>
        </td>
        <td>user132</td>
        <td></td>
        <td>1970-1-1</td>
    </tr>
    <script>
        console.log('item.id=', '1');

        function getId() {
            console.log('func getId->item.id=', '1');
        }
    </script>
    
  
   ····
    
    <tr>
        <td>5
            <a onclick="getId()" style="background-color: orange; cursor: pointer;">点击我获取id</a>
        </td>
        <td>user11111</td>
        <td></td>
        <td>1990-3-21</td>
    </tr>
    <script>
        console.log('item.id=', '5');

        function getId() {
            console.log('func getId->item.id=', '5');
        }
    </script>
    
    <tr>
        <td>6
            <a onclick="getId()" style="background-color: orange; cursor: pointer;">点击我获取id</a>
        </td>
        <td>青山威廉</td>
        <td></td>
        <td>2000-5-6</td>
    </tr>
    <script>
        console.log('item.id=', '6');

        function getId() {
            console.log('func getId->item.id=', '6');
        }
    </script>

由此可以知道大家都是调用的getId(),所以电脑识别的话无法分清楚到底谁是谁。因此我们解决的前提就是要给它们一个"特定的id"号。

我的解决方案

<!--
 * @Description: 测试模版继承页面
 * @Autor: 365JHWZGo
 * @Date: 2022-02-15 17:01:24
 * @LastEditors: 365JHWZGo
 * @LastEditTime: 2022-03-09 20:03:43
-->
{% extends "test_base.html" %} {% block content %}
<table class="content">
    <th>
        <td>用户id</td>
        <td>姓名</td>
        <td>性别</td>
        <td>出生日期</td>
    </th>
    {% for item in user %}
    <tr>
        <td>{{item.id}}
            <a onclick="getId(this)" alt="{{item.id}}" style="background-color: orange; cursor: pointer;">点击我获取id</a>
        </td>
        <td>{{item.username}}</td>
        <td>{{item.sex}}</td>
        <td>{{item.birthday}}</td>
    </tr>
    {% endfor %}
</table>
<script src="https://cdn.bootcss.com/jquery/2.1.2/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<script>
    function getId(a) {
        console.log('func getId->item.id=', $(a).attr('alt'));
    }
</script>
{% endblock content %}

最重要的是这里!!!使用this特殊指向该item,获取alt属性里的id

function getId(a) {
   console.log('func getId->item.id=', $(a).attr('alt'));
}

这样就可以获取到对应的id了
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

365JHWZGo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值