题目
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了