javascript - 从点击的元素获取数据 Django

标签 javascript html django django-views click

我有一个页面,用户可以在其中选择要添加到其团队中的人员。 页面的一侧是要选择的人员列表。 当用户单击“添加到团队”时,它会转到右侧,我们将在其中显示所选人员的列表。

我不明白如何从 django 的 View 中获取所选一侧的数据..

例如左边:

<div class="card-body" id="team-list">                   
   <p class="card-text">Select today's teammates:</p>
   <ul class="list-group list-group-flush">
      {% for tech in techs %}
         <li class="list-group-item">
            <span class="name" name="{{tech.id}}">{{tech.name}}</span>
            <span class="move" style="float: right;">Add to the team</span>
         </li>
      {% endfor %}

右边:

<div class="card-body" id="selected-list">
  <h3 class="title">You have selected the following teammates for today: </h3>
  <ul class="list-group list-group-flush" style="list-style-type: none;">

  </ul>
</div>

点击是由一个小的js点击事件处理的,如下所示:

    var selected = document.querySelector('#selected-list ul');
    var team = document.querySelector('#team-list ul');
function clickHandlerTeam(e){


        if(e.target.classList.contains('move')){
            if (e.target.textContent == 'Add to the team'){
                console.log('changing add');
                e.target.textContent ='Remove from the team';
                selected.appendChild(e.target.parentNode);
            } else {
                console.log('changing remove');
                e.target.textContent = 'Add to the team';
                team.appendChild(e.target.parentNode);
            }    

        console.log('****************');

        }
        return;
}

感谢您的帮助

最佳答案

{{ selected_techs=[] }}
<div class="card-body" id="team-list">                   
   <p class="card-text">Select today's teammates:</p>
   <ul class="list-group list-group-flush">
      {% for tech in techs %}
         <li class="list-group-item">
            <span class="name" name="{{tech.id}}">{{tech.name}}</span>
            <span class="move" onclick="{{ selected_techs.append(tech) }}" style="float: right;">Add to the team</span>
         </li>
      {% endfor %}
</ul>
</p>
</div>

<div class="card-body" id="selected-list">
  <h3 class="title">You have selected the following teammates for today: </h3>
  <ul class="list-group list-group-flush" style="list-style-type: none;">
      {% for tech in selected_techs %}
         <li class="list-group-item">
            <span class="name" name="{{tech.id}}">{{tech.name}}</span>
         </li>
      {% endfor %}
  </ul>
</div>

我认为这应该可以解决您的问题。 只需记住添加

编辑1:

试试这个

{% with selected_techs=[] %}
<div class="card-body" id="team-list">                   
   <p class="card-text">Select today's teammates:</p>
   <ul class="list-group list-group-flush">
      {% for tech in techs %}
         <li class="list-group-item">
            <span class="name" name="{{tech.id}}">{{tech.name}}</span>
            <span class="move" onclick="{% selected_techs.append(tech) %}" style="float: right;">Add to the team</span>
         </li>
      {% endfor %}
</ul>
</p>
</div>

<div class="card-body" id="selected-list">
  <h3 class="title">You have selected the following teammates for today: </h3>
  <ul class="list-group list-group-flush" style="list-style-type: none;">
      {% for tech in selected_techs %}
         <li class="list-group-item">
            <span class="name" name="{{tech.id}}">{{tech.name}}</span>
         </li>
      {% endfor %}
  </ul>
</div>
{% endwith %}

关于javascript - 从点击的元素获取数据 Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61312798/

相关文章:

django - 数据驱动的下拉菜单 Django

python - 我如何在Python中的函数参数中传递实例属性

python - 评级模型 Django

javascript - 如何遍历JSON数组?

javascript - 将元素存储在数组中,然后在 jquery 中使用它

html - 粘性标题位置多语言网站

javascript - 将响应列表从水平更改为垂直 CSS

JavaScript将纯文本转换为链接&&表情符号

javascript - 使用 CasperJS 填写表单

html - 折叠表格中的行