我有以下动态生成的表单:
var data = [
{Id: "1", },
{Id: "2", },
{Id: "3", },
{Id: "4", },
{Id: "5", },
{Id: "6", },
];
$(document).on('click', '.dad-pagamento', function() {
var linha = ``;
for (var x = 0; x < data.length; x++) {
linha += `<form method="POST" action="" enctype="multipart/form-data" class="row g-3 instarefa">
<div class="col-2">
<input type="text" class="form-control" name="id_tareff[]" value="${data[x].Id}">
</div>
<div class="col-7">
<label for="obsposic" class="form-label">Observações </label>
<textarea rows="3" class="form-control aarea" name="obsposic[]"></textarea>
</div>
<div class="col-1">
<button type="button" class="btn-wide btn btn-primary" onclick="reg_tarefa();"> TRATADO <i class="metismenu-icon pe-7s-paper-plane"></i></button>
</div>
</form>
<hr class="linha"/>`;
$(".pagmfalta").html(linha);
$('#minhaDiv1').show();
}
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/pixeden-stroke-7-icon@1.2.3/pe-icon-7-stroke/dist/pe-icon-7-stroke.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>
<button type="button" class="btn btn-info dad-pagamento" style="float: right; margin-right: 5%; margin-top: 4%;"><i class="metismenu-icon pe-7s-search"></i> Consultar </button>
<section id="s1">
<div style="display:none" id="minhaDiv1">
<div class="row pagmfalta">
</div>
</div>
</section>
到目前为止一切顺利。我的问题是现在发送值 到php来插入到数据库中。我是这样做的:
function reg_tarefa()
{
var id_tareff = [];
$("input[name='id_tareff[]']").each(function() {id_tareff.push(this.value)});
var obsposic = [];
$("textarea[name^='obsposic[]']").each(function() {obsposic.push($(this).val())});
var dadosajax = {
'id_tareff[]' : id_tareff,
'obsposic[]' : obsposic
}
$.ajax({
url: 'regtarefa.php',
type: 'POST',
cache: false,
data: dadosajax,
success: function(result2){
}
});
}
例如,我想点击第二个按钮,其中id = 2,只发送关于该id的数据。当我这样做时,它发送所有id的数据。你能帮忙吗?
1条答案
按热度按时间sd2nnvve1#
更改按钮以将其自身作为参数传递给
reg_tarefa
:onclick="reg_tarefa(this);"
.然后将函数更改为使用相对于按钮的DOM导航来查找同一表单中的输入。