Javascript 如何从特定输入中获取输入值<;tr>;使用jQuery标记并求和所有值?
我有一些来自ajax请求的字段。现在我想总结所有字段,但不起作用。请帮我解决这个小问题。下面是我的代码Javascript 如何从特定输入中获取输入值<;tr>;使用jQuery标记并求和所有值?,javascript,jquery,Javascript,Jquery,我有一些来自ajax请求的字段。现在我想总结所有字段,但不起作用。请帮我解决这个小问题。下面是我的代码 $('.tree-id').click(function(e) { var id = $(this).data('id'); $('#createtreeModal form #tree_id').val(id); $.ajax({ type: 'GET
$('.tree-id').click(function(e) {
var id = $(this).data('id');
$('#createtreeModal form #tree_id').val(id);
$.ajax({
type: 'GET',
url: '/admin/tree/tree-info/' + id,
dataType: 'JSON',
success: function(response) {
// console.log(response);
var html = '';
$.each(response.tree_name, (index, res) => {
if (res != null)
html += `<tr data-index="${index}">
<td><input type="text" class="form-control" name="tree_name[]" value="${res}"></td>
<td><input type="text" class="form-control" name="quantity[]" value="${response.quantity[index]}"></td>
<td><input type="text" class="form-control" name="height[]" value="${response.height[index]}"></td>
<td><input type="text" class="form-control" name="round[]" value="${response.round[index]}"></td>
<td><input type="text" class="form-control" readonly name="total[]" value="${response.total[index]}"></td>
</tr>`;
});
$('#tree_info').html(html);
}
});
});
$(document).on('blur', '#tree_info tr input', function(e) {
// console.log($(this).closest('tr input'))
$(this).closest('tr input').each(function() {
console.log($(this).val());
})
});
$('.tree id')。单击(函数(e){
var id=$(this.data('id');
$('#createTreeModel form#tree_id').val(id);
$.ajax({
键入:“GET”,
url:'/admin/tree/tree info/'+id,
数据类型:“JSON”,
成功:功能(响应){
//控制台日志(响应);
var html='';
$.each(response.tree_name,(index,res)=>{
如果(res!=null)
html+=`
`;
});
$('#tree_info').html(html);
}
});
});
$(文档).on('blur','tree#u info tr input',函数(e){
//console.log($(this.closest('tr input'))
$(this).最近('tr input')。每个(函数(){
log($(this.val());
})
});
这段代码只返回我选择的一个字段,但我需要所有输入
任何解决方案,谢谢 尝试替换
$(this).closest('tr input')。每个(函数(){
log($(this.val());
});
与
$(this).closest('tr').find('input').each(function(){
log($(this.val());
});
正如下面的评论中所提到的,上述内容并不能直接回答您的问题。为了总结所有的价值观,我假设下面的方法是可行的
$(文档).on('blur','tree#u info tr input',函数(e){
设sumValue=0;
$(this).closest('tr').find('input').each(function()){
sumValue+=$(this.val();
});
console.log('总和值为',sumValue);
});
说得清楚一点,您希望对每个列中的所有值进行汇总?@Rorymcrossan yesHow是否按照OP的要求对所有值进行汇总?@Rorymcrossan it Works您的问题要求“汇总值”,而不仅仅是显示值。如果此答案解决了您的问题,则您的问题不准确,需要进行编辑。@RoryMcCrossan您是对的,我没有直接回答问题。我已经更新了我的答案。谢谢