javascript - 如何在满足特定条件时退出 jQuery .on() 函数中的函数

标签 javascript jquery

我有以下 javascript/jquery 代码。

我是一个 javascript 新手,所以如果这个问题非常容易解决,我深表歉意。 如果出现错误或在服务器上找不到 key ,我想退出此 .on('click') 函数。单击绑定(bind)会在表单上触发发布请求,因此如果不满足这两个条件,我想继续运行它。我检查了其他示例,并尝试了很多东西,例如 return.stopPropagation 等。

我该怎么做?我尝试了 return;return false; 但它仍然刷新页面并触发表单上的 post 请求。

我做错了什么?

$( document ).ready(function() {

    var validation = false;
    $('#submitform').on('click', function(e) {

        if (validation === true) {
            validation = false; // reset flag
            return;// let the event bubble away
        }

        if ($('#street_number') == "" && $('#route') == "" && $('#lat') == "" && $('#lon') == ""
                && $('#administrative_area_level_1') == "") {
            alert("Unknown Address");
            validation = false;
            e.preventDefault();
            return false;
        }
        else {
            var data_dict = {
                // Number
                'street_number': $('#street_number').val(),
                // Address
                'address': $('#route').val(),
                // City
                'city': $('#locality').val(),
                // postal code
                'postal_code': $('#postal_code').val(),
                // State
                'state': $('#administrative_area_level_1').val(),
                // Country
                'country': $('#country').val()
            };
            $.ajax({
                url: '/zillow_check/',
                data: data_dict,
                method: 'POST'
            }).then(function (response) {
                console.log("Checking for Zillow ID...");
                if (response.error) {
                    console.error("There was an error " + response.error);
                    $('.dashboard-main__appartment--input').addClass('serverError');
                    validation = false;
                    e.stopImmediatePropagation();
                    return;
                } else {
                    console.log("Zillow key: " + response);
                    if (response == 'No zillow ID found.') {
                        // You can change this to whatever you like.
                        alert("Can't add this object. It does not exist on Zillow. Check the Address and try again.");
                        validation = false;
                        e.stopImmediatePropagation();
                        return;

                    }
                    else if (isNaN(response) == false ) {
                        validation = true;
                        $('#propertyform').submit()
                    }
                }

            });

        }
    });
});

最佳答案

您需要立即防止所有情况下的默认情况。

Ajax 是异步的,因此您不能等待它完成来尝试阻止默认情况……但已经太晚了。

$('#submitform').on('click', function(e) {
    e.preventDefault();
    // no need for other instances of preventDefault() beyond here or for `stopImmediatePropagation()`

    // your other code

})

关于javascript - 如何在满足特定条件时退出 jQuery .on() 函数中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43182635/

相关文章:

javascript - 覆盖 Google Analytics 中的着陆页跟踪

javascript - 如何使用jCarousel放大 slider ?

javascript - 简化 setInterval

javascript - 如何在onclick函数html中传递字符串

javascript - `x in y` 与 `y[x]` 之间的区别

Javascript - 将计算值输入发票的文本区域

javascript - 通过脚本文件添加 jquery 脚本

jquery - 如何在 FullCalendar 中使用 'eventMouseout' 回调?

jquery - 数据绑定(bind)单选按钮没有样式

javascript - 如何使用 jQuery 从多选选项中取消选择值?