我想将assign-btn
部分下的img
和p
的值分别更改为employees
部分中的img
和p
。以下是我迄今为止所能做到的:
代码:
$(document).ready(function() {
var img = '';
var name = '';
var check = false;
$(document).on('click', '.emp-row', function() {
$(this).find('.employees').on('click', function() {
img = $(this).find('.get-img').data('imgid');
name = $(this).find('.get-name').data('name');
check = true;
});
if (check) {
$(this).find('.set-img').attr('src', img);
$(this).find('.set-name').text(name);
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row emp-row">
<div class="col-md-12">
<div class="assign-va" data-bs-toggle="dropdown">
<div class="assign-btn">
<button type="button" class="btn dropdown-toggle dropdown-toggle-btn">
<div class='avatar-img-left'>
<img src='images/fe1.png' class='me-2 rounded-circle avatar avatar-lg set-img'>
</div>
<div class='avatar-text-right' style="margin-top: 0">
<p class='avatar-name set-name' style="text-align: left">
John Doe
</p>
</div>
</button>
</div>
</div>
<div class="dropdown-menu">
<div class="parent-employee">
<div class="employees" style="cursor: pointer">
<div class='avatar-img-left'>
<img src='images/fe2.png' data-imgid='images/fe2.png' class='me-2 rounded-circle avatar avatar-lg get-img'>
</div>
<div class='avatar-text-right'>
<p class='avatar-name get-name' data-name='Jasmine Undoe'>
Jasmine Undoe
</p>
<input type="hidden" name="mydata" class="mydata" value="">
<input type="hidden" class="empid" id="empid">
</div>
</div>
<div class="employees" style="cursor: pointer">
<div class='avatar-img-left'>
<img src='images/fe3.png' data-imgid='images/fe3.png' class='me-2 rounded-circle avatar avatar-lg get-img'>
</div>
<div class='avatar-text-right'>
<p class='avatar-name get-name' data-name='Jenne Redoe'>
Jenna Redoe
</p>
<input type="hidden" name="mydata" class="mydata" value="">
<input type="hidden" class="empid" id="empid">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row emp-row">
<div class="col-md-12">
<div class="assign-va" data-bs-toggle="dropdown">
<div class="assign-btn">
<button type="button" class="btn dropdown-toggle dropdown-toggle-btn">
<div class='avatar-img-left'>
<img src='images/fe1.png' class='me-2 rounded-circle avatar avatar-lg set-img'>
</div>
<div class='avatar-text-right' style="margin-top: 0">
<p class='avatar-name set-name' style="text-align: left">
John Doe
</p>
</div>
</button>
</div>
</div>
<div class="dropdown-menu">
<div class="parent-employee">
<div class="employees" style="cursor: pointer">
<div class='avatar-img-left'>
<img src='images/fe2.png' data-imgid='images/fe2.png' class='me-2 rounded-circle avatar avatar-lg get-img'>
</div>
<div class='avatar-text-right'>
<p class='avatar-name get-name' data-name='Jasmine Undoe'>
Jasmine Undoe
</p>
<input type="hidden" name="mydata" class="mydata" value="">
<input type="hidden" class="empid" id="empid">
</div>
</div>
<div class="employees" style="cursor: pointer">
<div class='avatar-img-left'>
<img src='images/fe3.png' data-imgid='images/fe3.png' class='me-2 rounded-circle avatar avatar-lg get-img'>
</div>
<div class='avatar-text-right'>
<p class='avatar-name get-name' data-name='Jenne Redoe'>
Jenna Redoe
</p>
<input type="hidden" name="mydata" class="mydata" value="">
<input type="hidden" class="empid" id="empid">
</div>
</div>
</div>
</div>
</div>
</div>
当我改变第一个emp-row
上的值时,一切似乎都在工作,然而,问题是,当我试图改变第二个emp-row
上的值时,第一行的值首先被分配,然后一切都正常工作。
有什么办法可以防止这种情况发生吗?
谢谢!
1条答案
按热度按时间ffx8fchx1#
你面临的挑战不止几个。
button
元素应仅包含短语内容,因此不包含div
或p
Ref: www.example.com 所以我改变了这些。let
来约束作用域。id
属性是唯一标识符,因此不允许重复。参考: www.example.com 所以我删除了它们。$(function() {
的推荐缩写形式$(document).ready(function()
event
,然后访问let $parentRow = $(event.delegateTarget);
,这是事件处理程序附加的元素$('.emp-row')