繁体   English   中英

没有使用JS的经验-如何使用甜蜜警报来确认表单提交?

[英]No experience with JS- how to use sweet alert to confirm a form submission?

我只是发现了甜蜜的警报,并希望以简单的形式确认来实现它,但是我受到了很多信息的轰炸,并且在运气不好的情况下试图理解JavaScript。

我想在这里使用成功消息(第三个示例),但是我不明白如何正确地实现这一点。

我也想在之后刷新页面,但是我需要先解决这个问题。

另外,是否可以使用甜蜜警报来验证空白字段的表单并显示错误消息?

我知道这一切都是非常基本的内容,对此我将不胜感激,因为我不太了解自己在做什么。

的HTML

<form id="form" class="topBefore" action="<echo htmlspecialchars">
          <div>
                <h5 class="desc">Please Enter Your Information</h5>
             <input type="text" placeholder="DONATION AMOUNT" />
             <input  type="text" placeholder="FIRST NAME">
             <input  type="text" placeholder="LAST NAME">
             <input  type="text" placeholder="ADDRESS" />
             <input type="text" placeholder="CITY" />
             <input type="text" placeholder="STATE" />
             <input type="text" placeholder="ZIP" />
            <input  type="email" placeholder="E-MAIL">
            <input type="text" placeholder="PHONE" />
            <h5>How did you find Flower Spark / All Hallows Guild?</h5>
            <input type="text" placeholder="How did you find Flower Spark?"/>
          <div>
            <h5>Select payment method:</h5>
            <div class="pad-bot">
            <select name="Field106" class="field select medium" tabindex="11"> 
              <option value="First Choice">Credit Card/PayPal</option>
              <option value="Second Choice">Check/Money Order</option>
            </select>
            </div>
          </div>
            <button id="submit">
              <span class="state">Submit</span>
            </button>
    </div>
</form>

JS

swal("Good job!", "You clicked the button!", "success")
$('#form').on('submit', function () {
  swal("Good job!", "You clicked the button!", "success")
});

要么

document.getElementById('submit').addEventListener('click', function () {
  swal("Good job!", "You clicked the button!", "success")
});

将此JavaScript添加到您的页面中,可以在表单之后,也可以包装在onload处理程序中:

$("#form").submit(function(e) {
    if(/* Validate form */) {
        swal("Good job!", "You clicked the button!", "success");
        setTimeout(function() {
            window.location = "redirect page";
        }, 3000);
    } else {
        //Display alert
    }

    e.preventDefault();
});

将要验证表单的任何代码(例如,检查缺少的字段)放置在if语句中,并将任何错误消息放置在else

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM