Sweetalert包和jQuery submit()无法正常工作

时间:2016-12-19 10:49:21

标签: javascript jquery laravel laravel-5.2 sweetalert

使用sweetalert库。这是我的表格:

function normaliser($str) {
    # convert letters with combining characters to a single unicode point
    $str = Normalizer::normalize($str, Normalizer::NFC);

    # transliterate unicode characters to ASCII
    setlocale(LC_CTYPE, 'fr_FR');
    $str = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $str);

    # convert to lowercase (since your string is now an ascii string, no need to use mb_strtolower)
    $str = strtolower($str);

    # remove html tags
    $str = strip_tags($str);

    # TODO: - deal with html entities
    #       - replace unwanted characters with an hyphen
    #       - trim leading and trailing hyphens

    return $str;
}

7 - 是商品ID。

然后我使用确认窗口(sweetalert):

<form id="delete-currency" style="display:inline;" method="POST" action="/admin/currency/7">
   <input type="hidden" name="_token" value="sIgpTKlPJ4Z3Co4daRwGpZ8rz10TCM6Ynre8sdsdsd">
   <input type="hidden" name="_method" value="DELETE">
   <button type="button" class="btn btn-danger btn-delete"><i class="fa fa-trash-o" aria-hidden="true"></i></button>
</form>

我有一个资源控制器,它接受带货币ID的删除方法。但是,如果我将sweetalert与jquery <script type="text/javascript"> $('.btn-delete').click(function() { swal({ title: "Are you sure?", text: "blablabla!", type: "warning", showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: "Yes, i am sure", closeOnConfirm: false, cancelButtonText: "Cancel" }, function(isConfirm) { $('#delete-currency').submit(); }); }); </script> 一起使用,它总是发送“1”而不是实际的submit()

我的控制器:

$currency->id

dd($ id)始终显示“1”。

routes.php文件:

public function destroy($id)
{
    dd($id);
    $currency = Currency::findOrFail($id);
    $currency->delete();
    alert()->success('asdasdsadad', 'Success')->persistent('close');
    return redirect('/admin/currency');
}

怎么了?

1 个答案:

答案 0 :(得分:2)

试试这个。

 $('.btn-delete').on('click',function(e){
    e.preventDefault();
    var form = $(this).parents('form');
swal({
    title: "Are you sure?",
     text: "blablabla!",
     type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes, delete it!",
    closeOnConfirm: false
}, function(isConfirm){
    if (isConfirm) form.submit();
});

})

我认为这会给你正确的价值。

希望有所帮助