我无法更新数据属性。我有一个课程.edf
,点击后会在dom中为点击的元素添加新的范围标记并更新数据属性data-clck
这是有用的
现在我希望当我点击其中一个新添加的span元素时,所有.edf
的数据属性data-clck
都应更新为0
值。 不工作
以下代码和fiddle if required:
var pans = "<span class='panok'></span><span class='panno'></span>";
var input1 = "<input name='vendor_service_item' type='text' class='in_cell_input' value='";
var input2 = "'>";
var bal;
$('.edf').click(function(){
var dis = $(this);
var clck = dis.attr('data-clck');
if(clck == 0){
$('.edf').each(function(){$(this).attr('data-clck','1');});
bal = dis.html().toString();
var nbal;
if (bal.includes('$')){nbal = bal.replace('$','')}
dis.html(input1+nbal+input2+pans);
dis.attr('data-clck','2');
}// if end
else if(clck == 2){}
else if(clck == 1){alert("Please deselect current cell to select other cells");}
});
$('.edf').on('click', '.panno', function(){
var tis = $(this);
tis.parent().html(bal);
$('.edf').each(function(){$(this).attr('data-clck','0');});
});//panno click end
.edf{text-align:center;position:relative;}
.edf:hover{cursor:pointer;cursor:hand;}
.panok{
position:absolute;
top:0;
right:0;
width:12px;
height:12px;
background-color: #6dc777;
}
.panno{
position:absolute;
bottom:0;
right:0;
width:12px;
height:12px;
background-color: #FF6666;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="tableizer-table" id="rebillable">
<thead>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Some Name</td>
<td class="edf" data-avsid="b0sds00001" data-clck='0'>1</td>
<td class="edf" data-avsid="bxsd000001" data-clck='0'>$10.22</td>
</tr>
</tbody>
</table>
答案 0 :(得分:1)
使用event.stopPropagation()
来阻止事件传播 - 所以当您点击panno
事件传播到时会发生什么edf 也被触发了。
一个指针 - 使用data('clck')
代替attr('data-clck')
。另请注意nbal
未正确评估 - 也已修复。
见下面的演示:
var pans = "<span class='panok'></span><span class='panno'></span>";
var input1 = "<input name='vendor_service_item' type='text' class='in_cell_input' value='";
var input2 = "'>";
var bal;
$('.edf').click(function() {
var dis = $(this);
var clck = dis.data('clck');
if (clck == 0) {
$('.edf').each(function() {
$(this).data('clck', '1');
});
bal = dis.html().toString();
var nbal = bal.replace('$', ''); // CHANGED THIS
/*if (bal.includes('$')) {
nbal = bal.replace('$', '')
}*/
dis.html(input1 + nbal + input2 + pans);
dis.data('clck', '2');
} // if end
else if (clck == 2) {} else if (clck == 1) {
alert("Please deselect current cell to select other cells");
}
});
$('.edf').on('click', '.panno', function(e) {
e.stopPropagation(); // ADDED THIS
var tis = $(this);
tis.parent().html(bal);
$('.edf').each(function() {
$(this).data('clck', '0');
});
}); //panno click end
.edf {
text-align: center;
position: relative;
}
.edf:hover {
cursor: pointer;
cursor: hand;
}
.panok {
position: absolute;
top: 0;
right: 0;
width: 12px;
height: 12px;
background-color: #6dc777;
}
.panno {
position: absolute;
bottom: 0;
right: 0;
width: 12px;
height: 12px;
background-color: #FF6666;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="tableizer-table" id="rebillable">
<thead>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Some Name</td>
<td class="edf" data-avsid="b0sds00001" data-clck='0'>1</td>
<td class="edf" data-avsid="bxsd000001" data-clck='0'>$10.22</td>
</tr>
</tbody>
</table>