我有一个表单,可以获取检查框及其相应的输入数字,当我单击表单的按钮时,我使用javascript收集了文档的所有值,但没有将脚本的值传递给ajax并转到php帮助表。
表格:
<div class="form-check">
<input class="form-check-input" name="ingrediente" type="checkbox" id="ing$id" value="$id">
<label class="form-check-label" for="ing$id">" . utf8_encode($ingrediente) . "</label>
</div>
<div class="quiantitynice" id="quiantitynice$id" style='position:relative;display:none'>
<input type=\"number\" name=\"quantity\" id="quiantitynice$id" min="1" value="" Style="width:45Px" placeholder="1">
</div>
<script>
$(document).ready(function(){
$('[name="ingrediente"]').on('change',function () {
var _thisVal = $(this).val();
if (this.checked) {
$("#quiantitynice" + _thisVal).show();
} else {
$("#quiantitynice" + _thisVal).hide();
}
});
});
<button class="btn btn-lg btn-wy" onclick="addcarto('');">AGREGAR</button>
编码到javascript文件中按钮功能的位置:
function addcarto() {
var extra = "";
var extras = ["NINGUNO"];
//Cantidad
var amount = "";
var quantity = document.getElementsByName("quantity");
//Cantidad
var chkbExtras = document.getElementsByName("ingrediente");
for (i = 0; i < chkbExtras.length; i++) {
if (chkbExtras[i].checked) {
extra = chkbExtras[i].value;
extras[i] = extra;
//cantidad
amount = quantity[i].value;
quantities[i] = amount;
//cantidad
}
}
var valParam = JSON.stringify(extras);
var valParam2 = JSON.stringify(quantities);
$.ajax({
type: 'post',
url: 'php/addCart2.php',
//data: 'cantidades=' + quantities + '&opcion=' + 'nuevo' + '&extras=' + extras,
data: { cantidades: valParam, extras: valParam2},
contentType: 'application/json; charset=utf-8',
success: function (response) {
//document.getElementById('btnPedir').innerHTML = "<img src='img/icono-pedido0.png' width='40'><span class='badge badge-pill badge-danger'>" + data + "</span>";
alert(response.status);
},
error: function () {
alert("error");
}
});
return false;}
从ajax接收数据的文件代码: