我有div复选框,当我单击它时需要它得到仅单击它的div的内部文本
var serviceChecked = document.querySelectorAll('.checkbox__toggle');
var serviceTitle = document.querySelectorAll('.serveTitle');
serviceChecked.forEach(function(listItem){
listItem.addEventListener('click', function(e){
for (var t = 0; t < serviceTitle.length; t++) {
alert(serviceTitle[t].innerText);
}
});
});
<div class="checkdivwrp">
<input class='checkbox__toggle' type='checkbox'>
<div class="serveTitle">title 1</div>
</div>
<div class="checkdivwrp">
<input class='checkbox__toggle' type='checkbox'>
<div class="serveTitle">title 2</div>
</div>
<div class="checkdivwrp">
<input class='checkbox__toggle' type='checkbox'>
<div class="serveTitle">title 3</div>
</div>
最佳答案
您需要使用on('click'
并检查该复选框是否已选中。如果选中,请使用$(this).parent().find('.serveTitle').text()
获取内部文本:
$(".checkbox__toggle").on('click', function() {
if ($(this).is(':checked'))
console.log($(this).parent().find('.serveTitle').text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="checkdivwrp">
<input class='checkbox__toggle' type='checkbox'>
<div class="serveTitle">title 1</div>
</div>
<div class="checkdivwrp">
<input class='checkbox__toggle' type='checkbox'>
<div class="serveTitle">title 2</div>
</div>
<div class="checkdivwrp">
<input class='checkbox__toggle' type='checkbox'>
<div class="serveTitle">title 3</div>
</div>
关于javascript - 我有div复选框,当我单击它时需要它得到div的内部文本,我只单击它,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45660821/