从jQuery重复错误代码

时间:2015-07-08 07:12:54

标签: jquery

以下代码工作正常,但当我点击用户登录表单上的提交时,用户名和密码不正确,我收到此错误:

  

错误的信息

然后我再次使用不正确的值单击提交,并反复重复此错误消息。因此我的div奇怪地扩展了。我怎样才能解决这个问题?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
$("#giris").submit(function (event) {
    var kullanici_adi = $("#kullanici_ad").val();
    var sifre = $("#sifre").val();
    $.ajax({
        type: "POST",
        url: "girisislemi.php",
        data: {
            kullanici_adi: kullanici_adi,
            sifre: sifre
        },
        dataType: "json",
        success: function (data) {
            if (data.tip === 'yonetici') {
                window.location.href = "yonetici.php";
            }
            if (data.tip === 'kullanici') {
                window.location.href = "kullanici.php";
            }
            if (data.tip === 'error') {
                $('input[type=text]').css("border", "3px solid red");
                $('input[type=password]').css("border", "3px solid red");
                $('#giris').after("<font><p>Incorrect Information </p><font>");
                $('font').css("color", "red");
            }

        }

    });
    event.preventDefault();
});

1 个答案:

答案 0 :(得分:1)

问题是由于您使用了return,每次运行时都会添加新内容。相反,您可以检查是否已显示错误:

after()

另请注意,if (data.tip === 'error') { $('input[type=text], input[type=password]').addClass('login-error'); if (!$('p.login-error').length) $('#giris').after('<p class="login-error">Incorrect Information </p>'); } 元素已经过时,不应再使用了,您应该在样式表中的类中设置样式规则,并使用add / removeClass而不是设置inline { {1}}。

<font />