jquery附加而不是警告消息

时间:2013-04-11 14:54:41

标签: jquery forms hide prepend

我想在div中显示一条消息而不是提醒它,然后将其隐藏在click/blur上 由于输入错误,警报弹出。我需要在div中显示消息而不是警告。

类似:

$('form.form-calculator input').on('change click', function() {
        if ($(this).val() == 0) {
            var div = $("#AlertMessage");
            div = $("<div id='AlertMessage' onclick='$(this).hide();'></div>");
            $("body").prepend(div);
            this.value=0;
        }
        calc_total();
    });

使用提醒

 $(function () {
       $('.form input').on('change click focus', function () {
          if ($(this).val() == 0) {
            alert('HELLO');
            this.value = 0;
          }
          calc_total();
       });
    });

2 个答案:

答案 0 :(得分:0)

假设你的两段代码都工作(因为你没有要求对这些代码进行故障排除),你可以将它们组合成一个这样的代码:

 $(function () {
       $('.form input').on('change click focus', function () {
          if ($(this).val() == 0) {
            div = $("<div id='AlertMessage' onclick='$(this).hide();'>HELLO</div>");
            $("body").prepend(div);
            this.value = 0;
          }
          calc_total();
       });
    });

答案 1 :(得分:0)

$(function () {
    $('.form input').on('focusout', function () {
        if ($(this).val() == 0) {
            showError('Hello');
        } else {
            $("#AlertMessage").remove();
        }
        calc_total();
    });

    function showError(text) {
        $("#AlertMessage").remove();
        $('<div/>', {
            id: 'AlertMessage',
            text: "Alert! It can't be 0"
        }).prependTo('body');
        // Close on click
        $('body').delegate('#AlertMessage', 'click', function () {
            $(this).hide();
        });
    }
});