【问题标题】:Redirect whit SweetAlert2使用 SweetAlert2 重定向
【发布时间】:2020-01-17 23:36:26
【问题描述】:

在用户单击甜蜜警报中的确定按钮后,我的重定向页面出现问题。我不明白我必须在哪里插入代码以及哪些代码。我希望当用户按下确定时,在表单成功后被重定向到...例如“google.com”。

我尝试使用网站上的解决方案,但它们不起作用或我插入错误 感谢大家的帮助

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script>
	$(function() {
		$(document).ajaxStop($.unblockUI);
		$('form').on('submit', function(e) {
			e.preventDefault();
			var response = grecaptcha.getResponse();
			if(response.length == 0) {
				//reCaptcha not verified
				alert("Verifica di non essere un robot!");
				return false;
			} else {
				//reCaptch verified
				$.blockUI({
					message: '<p><img src="img/Eclipse-1s-134px.svg" alt="">Ci siamo...non uscire</p>'
				});
				var formData = new FormData(this);

				$.ajax({
					type: 'post',
					url: 'mailer.php',
					data: formData,
					cache: false,
					dataType: 'json',
					contentType: false,
					processData: false,
					mimeType: "multipart/form-data",
					xhr: function () {
						//upload Progress
						var xhr = $.ajaxSettings.xhr();
						if (xhr.upload) {
							xhr.upload.addEventListener('progress', function (event) {
								var percent = 0;
								var position = event.loaded || event.position;
								var total = event.total;
								if (event.lengthComputable) {
									percent = Math.ceil(position / total * 100);
								}
								//update progressbar
								$('.progress-bar').animate({
									width: percent + '%'
								}, {
									duration: 100
								});
							}, true);
						}
						return xhr;
					},
					success: function (response) {
						if (response.success) {
		 						Swal.fire(
								   'Perfetto!',
								   'Il tuo locale è registrato!',
								   'success'
								 );
							$('form').trigger("reset");
							$('.progress-bar').animate({
								width: '10%'
							}, {
								duration: 1000
							});
					 
						} else {
							alert(response.message);
						}
					},
					error: function (response) {
						 				Swal.fire({
						 icon: 'error',
						 title: 'Oops...',
						 text: 'Qualcosa è andato storto!',
						 footer: '<a href>Riprovaci!</a>'
						});
					}
				});
			}
		});
	});
</script>

【问题讨论】:

    标签: javascript ajax sweetalert sweetalert2


    【解决方案1】:

    我觉得一定要放在成功码之前,像这样:

    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
    <script>
        $(function() {
            $(document).ajaxStop($.unblockUI);
            $('form').on('submit', function(e) {
                e.preventDefault();
                var response = grecaptcha.getResponse();
                if(response.length == 0) {
                    //reCaptcha not verified
                    alert("Verifica di non essere un robot!");
                    return false;
                } else {
                    //reCaptch verified
                    $.blockUI({
                        message: '<p><img src="img/Eclipse-1s-134px.svg" alt="">Ci siamo...non uscire</p>'
                    });
                    var formData = new FormData(this);
    
                    $.ajax({
                        type: 'post',
                        url: 'mailer.php',
                        data: formData,
                        cache: false,
                        dataType: 'json',
                        contentType: false,
                        processData: false,
                        mimeType: "multipart/form-data",
                        xhr: function () {
                            //upload Progress
                            var xhr = $.ajaxSettings.xhr();
                            if (xhr.upload) {
                                xhr.upload.addEventListener('progress', function (event) {
                                    var percent = 0;
                                    var position = event.loaded || event.position;
                                    var total = event.total;
                                    if (event.lengthComputable) {
                                        percent = Math.ceil(position / total * 100);
                                    }
                                    //update progressbar
                                    $('.progress-bar').animate({
                                        width: percent + '%'
                                    }, {
                                        duration: 100
                                    });
                                }, true);
                            }
                            return xhr;
                        },
                        success: function (response) {
                            if (response.success) {
                                    Swal.fire(
                                       'Perfetto!',
                                       'Il tuo locale è registrato!',
                                       'success'
                                     );
                                $('form').trigger("reset");
                                $('.progress-bar').animate({
                                    width: '10%'
                                }, {
                                    duration: 1000
                                });
    
                                window.location.href = 'www.yourpage.com';
    
                            } else {
                                alert(response.message);
                            }
                        },
                        error: function (response) {
                                            Swal.fire({
                             icon: 'error',
                             title: 'Oops...',
                             text: 'Qualcosa è andato storto!',
                             footer: '<a href>Riprovaci!</a>'
                            });
                        }
                    });
                }
            });
        });
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多