如何在单个警报框中显示验证时的多条消息 - jquery

时间:2017-03-18 02:32:16

标签: jquery html

$(function()
{
$("#btnMessage").click(function()
{
    var name = $("#txtName").val();
    var phone = $("#txtPhone").val();
    var pizzaBase = $("#cboBase").val();
    var pizzaSize = $("input[name=radSize]:checked").val();
    message = "";


    if (name == "")
    {
        message += "--Enter a name\n";
    }

    if (phone == "")
    {
        message += "--Enter a valid phone number - 999-999-9999\n";
    }

    if (pizzaBase == "0")
    {
        message += "--Select a base pizza\n";
    }

    if (pizzaSize == undefined)
    {
        message += "--Select a pizza Size";

    }



    /*else 
    {
        message = name;

        alert(total);
    }*/


});

});

所以,如果我在验证过程中遗漏了一些内容。即如果我错过了名字和一切。如何使警报显示该消息变量? 在什么情况下if语句应该是什么?

1 个答案:

答案 0 :(得分:1)

$(function()
{
$("#btnMessage").click(function()
{
    var name = $("#txtName").val();
    var phone = $("#txtPhone").val();
    var pizzaBase = $("#cboBase").val();
    var pizzaSize = $("input[name=radSize]:checked").val();
    message = "";


    if (name == "")
    {
        message += "--Enter a name\n";
    }

    if (phone == "")
    {
        message += "--Enter a valid phone number - 999-999-9999\n";
    }

    if (pizzaBase == "0")
    {
        message += "--Select a base pizza\n";
    }

    if (pizzaSize == undefined)
    {
        message += "--Select a pizza Size";

    }



    if(message)
    {
       alert(total);
    } else {
      //Continue your processing
    }


});

});