【发布时间】:2018-04-24 09:39:18
【问题描述】:
我正在使用SweetAlert2 来显示效果很好的表单,但是我最近遇到了一个我无法解决的问题,我已经浪费了几天时间试图在 Google 上找到解决方案。
我正在使用 Sweet Alert 加载的 PHP 动态构建一个表单。该表单包含多个具有相同名称/ID 的复选框。我有 3 组多个复选框,因为它是动态表单,所以我不知道每组中有多少个复选框。
复选框集被命名为 invoice_details、invoice_hours 和 invoice_materials。每组可能没有复选框,或者可能有十个或更多。但是每组checkbox的名字都一样,所以invoice_details集合都命名为invoice_details。
如何将每组复选框的结果放入数组中并将它们传递给 Promise,以便我可以在 php 页面中处理表单?
<script type="text/javascript">
// SCRIPT TO ADD INVOICE
//Warning Message with function
$(document).on('click', '#addinvoice', function(){
swal({
title: 'Generate New Invoice',
html: '<?php echo tm_generate_invoice_form( $post->ID ) ?>',
showCancelButton: true,
confirmButtonText: 'Submit',
showLoaderOnConfirm: true,
buttonsStyling: true,
confirmButtonClass: 'btn btn-primary btn-lg',
cancelButtonClass: 'btn btn-lg',
preConfirm: function () {
return new Promise(function (resolve) {
resolve([
$('#invoice_number').val(),
$('#invoice_details').val(),
$('#invoice_hours').val(),
$('#invoice_materials').val(),
$('#invoice_custom_message').val(),
$('#jobid').val(),
$('#tm_add_invoice').val()
])
})
}
}).then(function(result){
swal(
"Invoice Created!",
"Invoice now ready to be sent to customer.",
"success"
);
$.ajax({
type: 'post',
url: '<?php echo admin_url( 'admin-ajax.php' ) ?>',
data: {
action: 'tm_add_invoice',
invoice_number: result[0],
invoice_details: result[1][0],
invoice_hours: result[2][0],
invoice_materials: result[3][0],
invoice_custom_message: result[4],
jobid: result[5],
tm_add_invoice: result[6]
},
success: function(data){
$('#invoices').html(data);
},
error: function(e){
alert("error " + e);
console.log(e);
}
});
});
});
</script>
【问题讨论】:
-
如果
swal()是thenable,为什么第二个swal()不是then'd? -
两个问题:(1)表单构建php - 是你自己修改的吗? (2) 你说“我不知道每组会有多少个复选框”和“复选框名称是
invoice_details、invoice_hours和invoice_materials”。但不清楚;每个集合都包含这三个复选框,还是有invoice_details集合、invoice_hours集合和invoice_materials集合? -
感谢 Roamer 的回复,是的,构建 php 的表单是我要修改的。有一个 invoice_details 集、一个 invoice_hours 集和一个 invoice_materials 集。但是一个集合可能没有复选框,或者它可能有十个或更多......
-
感谢您的回复。还有一个问题:在服务器端,您对未选中的复选框感兴趣吗?我问的原因是发送反映选中复选框的数据非常简单,但需要一些人为的设计来包含未选中的复选框。
-
不,未选中的复选框无关紧要。
标签: javascript arrays promise sweetalert2