简体   繁体   English

jQuery,否则,如果不起作用

[英]jQuery, else if isn't working

I'm sending data to my database using .ajax. 我正在使用.ajax将数据发送到数据库。 I've got two fields. 我有两个领域。 I'd like to check if either are empty and if so display an error. 我想检查其中一个是否为空,如果显示为错误。 The first if works fine. 第一个如果工作正常。 That message display, but the else if doesn't display. 该消息显示,但else if不显示。 What could be my problem? 我可能是什么问题? I'm not too familiar with javascript and iquery. 我对javascript和iquery不太熟悉。

$.ajax({
    type: "POST",
    url: "submit.php",
    data: data,
    cache: false,
     beforeSend: function() {
        if (!$.trim($("#cat").val())) {
            $(".fb-error").css( "display", "block" );
            $(".fb-error").html("<p>the cat field is empty</p>");
            xhr.abort();
        }

        else if (!$.trim($("#box").val())) {
            $(".fb-error").css( "display", "block" );
            $(".fb-error").html("<p>the box field is empty</p>");
            xhr.abort();
        }
    },
    success: function(html){
        $('textarea#box').val('');
        $("#box-wrap").prepend(html);
    }
});

You can't use else if you want to check both conditions. 如果要同时检查这两个条件,则不能使用else

var error = '';

if (!$.trim($("#cat").val())) {
        error = "<p>the cat field is empty</p>";
}

if (!$.trim($("#box").val())) {
        error += "<p>the box field is empty</p>";
}

if (error) {
        $(".fb-error").html(error);
        $(".fb-error").css( "display", "block" );
        xhr.abort();
}

In second if you have to append new html, to prevent overwriting already added message. 第二, if您必须附加新的html,以防止覆盖已添加的消息。

Use separate if statements for each condition; 对每个条件使用单独的if语句; else if only runs if the first if fails. else if仅在第一个if失败时运行。 Use a variable to accumulate the messages. 使用变量来累积消息。

 beforeSend: function() {
    var msg = '';
    if (!$.trim($("#cat").val())) {
        $(".fb-error").css( "display", "block" );
        msg = "<p>the cat field is empty</p>";
        xhr.abort();
    }
    if (!$.trim($("#box").val())) {
        $(".fb-error").css( "display", "block" );
        msg += "<p>the box field is empty</p>";
        xhr.abort();
    }
    $(".fb-error").html(msg);
},

I'd put the errors in an array, and display them if it's not empty 我将错误放入数组中,如果不为空,则显示它们

var errors = [];
if (!$.trim($("#cat").val())) {
    errors.push("<p>the cat field is empty</p>");
}

if (!$.trim($("#box").val())) {
    errors.push("<p>the box field is empty</p>");
}

if(errors.length){
    $(".fb-error").css( "display", "block" ).html(errors.join(''));
    xhr.abort();
}

Consolidate: 合并:

var error = false;
if (!$.trim($("#cat").val())) {
        $(".fb-error").append("<p>the cat field is empty</p>");
        error = true;
}

if (!$.trim($("#box").val())) {
        $(".fb-error").append("<p>the box field is empty</p>");
        error = true;
}

if (error) {
        xhr.abort();
        $(".fb-error").css( "display", "block" );
}

To debug can you switch around #box and #cat? 要调试,您可以在#box和#cat之间切换吗?

You are using an else, so if #cat is empty, you will not see the message "the box field is empty". 您正在使用else,因此如果#cat为空,则不会看到“框字段为空”的消息。 It will only show this message if #cat is not empty, and #box is. 仅当#cat不为空,而#box为空时,才显示此消息。

change #box to something you know doesn't exist (and cannot exist). 将#box更改为您知道的不存在(并且不存在)的内容。 perhaps there is a value in it. 也许其中有一个价值。

you could ouput the value of #cat and #box to the console to ensure that they are empty. 您可以将#cat和#box的值输出到控制台以确保它们为空。

console.log("$.trim($("#cat").val()="+$.trim($("#cat").val()); console.log("$.trim($("#box").val()="+$.trim($("#box").val()); console.log(“ $。trim($(”#cat“)。val()=” + $。trim($(“#cat”)。val()); console.log(“ $。trim($ (“ #box”)。val()=“ + $。trim($(”#box“)。val());

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

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