Javascript 如何通过单击span获取最近复选框项的ID 你好
单击范围项目时,需要复选框的id值。如何获取它?向上导航,然后搜索Javascript 如何通过单击span获取最近复选框项的ID 你好,javascript,jquery,html,jquery-ui,Javascript,Jquery,Html,Jquery Ui,单击范围项目时,需要复选框的id值。如何获取它?向上导航,然后搜索 $('span')。单击(函数(){ var parent=$(this).closest('.todo task');//向上导航 var input=parent.find('input[type=“checkbox”]”);//搜索 var id=input.attr('id'); console.log(id); }); 你好 jQuery: $('.todo remove')。最近('.todo task')。查找
$('span')。单击(函数(){
var parent=$(this).closest('.todo task');//向上导航
var input=parent.find('input[type=“checkbox”]”);//搜索
var id=input.attr('id');
console.log(id);
});代码>
你好
jQuery:
$('.todo remove')。最近('.todo task')。查找('input')。attr('id')
遍历到也包含复选框的已知父级,找到复选框,然后获取id属性
<div class="todo-task">
<input type="checkbox" id=' checkbox1' />
<label for='checkbox1 '> hellooooo <span class="todo-remove mdi-action-delete"></span>
</label>
</div>
jQuery导航到标签
级别,然后选择与选择器匹配的同级。最后,您可以使用获取实际值
$('span.todo-remove').on('click', function() {
var id = $(this).closest('.todo-task').find('input:checkbox').attr('id');
// do something with id
});
jsidle:
$(函数(){
$('.todo remove')。在('click',function()上{
var checkID=$(this).closest('div.todo-task').find(':checkbox').prop('id');
console.log(checkID);
});
});代码>
你好
$(".todo-remove").on('click', function() {
var id = $(this).parent().siblings("input[type='checkbox']").attr("id");
});