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

时间:2017-04-28 15:23:19

标签: javascript forms sweetalert

我刚刚发现了甜蜜的警报,并希望以简单的形式确认实施它,但我被信息轰炸,我正试图理解JavaScript,因为我没有运气。

我想使用成功消息here(第三个例子),但我不明白如何正确实现它。

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

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

我知道这一切都非常简陋,我非常感谢对此的一些帮助,因为我对我正在做的事情没有多少线索。

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")

2 个答案:

答案 0 :(得分:2)

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

OR

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

答案 1 :(得分:0)

将此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中您想要的任何错误消息。

相关问题