我的跨度是一个动态值(整数)。我需要将跨度范围内的值传递给我的输入。
jquery 应该可以实现,但我不知道如何实现。
<span class="id-label"></span>
<input type="hidden" id="label" name="label" value="" />
$(document).ready(function() {
var table = $('#example').DataTable();
$('#example tbody').on('dblclick', 'tr', function() {
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
} else {
table.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
$('.id-label').text($(this).closest('tr').find('td:first').text());
$('#classModal').modal('hide');
}
});
});
答案 0 :(得分:0)
鉴于问题中的代码,您正在检索一个值并将其设置为 text()
的 .id-label
,您可以使用相同的值并将其设置为 val()
您的目标输入:
$(document).ready(function() {
var table = $('#example').DataTable();
$('#example tbody').on('dblclick', 'tr', function() {
let $tr = $(this);
if ($tr.hasClass('selected')) {
$tr.removeClass('selected');
} else {
table.$('tr.selected').removeClass('selected');
$tr.addClass('selected');
$('#classModal').modal('hide');
let targetValue = $tr.find('td:first').text();
$('.id-label').text(targetValue);
$('#label').val(targetValue);
}
});
});
请注意,我删除了 .closest('tr')
,因为 this
已经指代 tr
。