【问题标题】:How to pass array in Sweet Alert 2 Promise如何在 Sweet Alert 2 Promise 中传递数组
【发布时间】:2018-04-24 09:39:18
【问题描述】:

我正在使用SweetAlert2 来显示效果很好的表单,但是我最近遇到了一个我无法解决的问题,我已经浪费了几天时间试图在 Google 上找到解决方案。

我正在使用 Sweet Alert 加载的 PHP 动态构建一个表单。该表单包含多个具有相同名称/ID 的复选框。我有 3 组多个复选框,因为它是动态表单,所以我不知道每组中有多少个复选框。

复选框集被命名为 invoice_detailsinvoice_hoursinvoice_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_detailsinvoice_hoursinvoice_materials”。但不清楚;每个集合都包含这三个复选框,还是有invoice_details 集合、invoice_hours 集合和invoice_materials 集合?
  • 感谢 Roamer 的回复,是的,构建 php 的表单是我要修改的。有一个 invoice_details 集、一个 invoice_hours 集和一个 invoice_materials 集。但是一个集合可能没有复选框,或者它可能有十个或更多......
  • 感谢您的回复。还有一个问题:在服务器端,您对未选中的复选框感兴趣吗?我问的原因是发送反映选中复选框的数据非常简单,但需要一些人为的设计来包含未选中的复选框。
  • 不,未选中的复选框无关紧要。

标签: javascript arrays promise sweetalert2


【解决方案1】:

忘记 ID。使用格式为name="xxx[]"name 属性构建表单。

例如:

<form id="myForm">
    <input name="invoice_number" value="invoice_no_000" /><br/>
    <br/>
    <input type="checkbox" name="invoice_details[]" value="abc" /><br/>
    <input type="checkbox" name="invoice_details[]" value="bcd" /><br/>
    <br/>
    <input type="checkbox" name="invoice_hours[]" value="cde" /><br/>
    <input type="checkbox" name="invoice_hours[]" value="def" /><br/>
    <input type="checkbox" name="invoice_hours[]" value="efg" /><br/>
    <input type="checkbox" name="invoice_hours[]" value="fgh" /><br/>
    <br/>
    <input type="checkbox" name="invoice_materials[]" value="ghi" /><br/>
    <input type="checkbox" name="invoice_materials[]" value="hij" /><br/>
    <input type="checkbox" name="invoice_materials[]" value="ijk" /><br/>
    <input type="checkbox" name="invoice_materials[]" value="jkl" /><br/>
    <input type="checkbox" name="invoice_materials[]" value="klm" /><br/>
    <input type="checkbox" name="invoice_materials[]" value="lmn" /><br/>
    <input type="checkbox" name="invoice_materials[]" value="mno" /><br/>
    <br/>
    <!-- other fields as necessary -->
    <br/>
    <input type="submit" />
</form>

每个集合中的元素不一定是相邻的。交错的元素将以相同的方式序列化。

现在您可以简单地使用 jQuery 的.serialize() 序列化表单。

'preConfirm': function () {
    return Promise.resolve($('#myForm').serialize());
}

正如文档所说:

只有“成功的控件”被序列化为字符串。由于未使用按钮提交表单,因此未序列化提交按钮值。对于要包含在序列化字符串中的表单元素的值,该元素必须具有 name 属性。复选框和单选按钮(“radio”或“checkbox”类型的输入)的值仅在它们被选中时才包含在内。来自文件选择元素的数据未序列化。

因此,根据选中的复选框,生成的序列化(添加换行符)可能是:

invoice_number%5B%5D=invoice_no_000&
invoice_details%5B%5D=bcd&
invoice_hours%5B%5D=cde&
invoice_hours%5B%5D=fgh&
invoice_materials%5B%5D=hij&
invoice_materials%5B%5D=jkl&
invoice_materials%5B%5D=klm&
invoice_materials%5B%5D=lmn

在服务器端,PHP 会将数据视为:

  • $_POST['invoice_number'] : 'invoice_no_000'
  • $_POST['invoice_details'] : ['bcd']
  • $_POST['invoice_hours'] : ['cde', 'fgh']
  • $_POST['invoice_materials'] : ['hij', 'jkl', 'klm', 'lmn']

我怀疑这是必需的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多