0

我正在使用SweetAlert.js替换典型alert();功能。触发顺序存在问题,在用户尝试提交不完整的表单后,我无法让它继续存在。在 jsFiddle 的“显示”模式下,警报会在页面加载时触发。在我设置的实际页面中,它根本不会触发,但警报文本显示在页面底部。

jsFiddle:http: //jsfiddle.net/mattography/d8s7rm19/1/

$("#submit").click(function () {
if ('localStorage' in window && window['localStorage'] !== null) {
    try {
        //Get stored data
        var users = JSON.parse(localStorage.getItem('users'));

        if (users === null) {
            //If no stored data, create empty array
            //users = [];
            sweetAlert("Oops...", "Something went wrong!", "error");
        }

        //Create new user entry
        user = {
            name: $(".name").val(),
            phone: $(".phone").val(),
            website: $(".website").val(),
            contact: $(".contact").val()
        };

        //Add new entry to stored array
        users.push(user);

        //Save array
        localStorage.setItem("users", JSON.stringify(users));

        alert("The data was saved.");
        return true;

    } catch (e) {
        if (e == QUOTA_EXCEEDED_ERR) {
            alert('Quota exceeded!');
        }
    }
    } else {
        alert('Cannot store user preferences as your browser do not support local storage');
    }
});
4

1 回答 1

0

You need to prevent the link event from processing. In this case, use:

event.preventDefault()
// and
return false;

See the updated solution: http://jsfiddle.net/d8s7rm19/2/

于 2015-01-19T03:38:49.447 回答