0

我有一个文本框。它的名字是电话号码。如果 len(输入值)=0,我想做一个弹出窗口。当我做一个标签它不起作用。(我在调试模式下查看)当我在另一个已经有效的 Jq 脚本中执行此操作时。它可以工作,但弹出窗口只停留几百万秒,所以我无能为力。我是编程新手,我还在学习。如果你帮助我,我会很高兴。谢谢。

<script type="text/javascript">
    $('#PhoneNumber').bind('keypress', function (e) {
        if (e.keyCode == 13) {
            var test = $('#PhoneNumber').val().length;
            if (test == 0) {
                alert('At Least');
                /*  $('a.login-window').one(function () {               
                    var loginBox = $(this).attr('href');
                    //Fade in the Popup and add close button
                    $(loginBox2).fadeIn(300);
                    //Set the center alignment padding + border
                    var popMargTop = ($(loginBox).height() + 24) / 2;
                    var popMargLeft = ($(loginBox).width() + 24) / 2;
                    $(loginBox).css({
                        'margin-top': -popMargTop,
                        'margin-left': -popMargLeft
                    });
                    // Add the mask to body
                    $('body').append('<div id="mask"></div>');
                    $('#mask').fadeIn(300);
                    return false;
                });

                // When clicking on the button close or the mask layer the popup closed
                $('a.close, #mask').live('click', function () {
                    $('#mask , .login-popup').fadeOut(300, function () {
                        $('#mask').remove();
                    });
                    return false;
                });*/    
            }
            else 
            {
                alert('At Least');
                $("#PhoneNumber").val("");
                $('#PhoneNumber').focus();
            }
        }
    });
</script>
4

1 回答 1

0

始终将您的 jquery 代码放入:

$(document).ready(function() {
  //Your code 
});

这可以确保在将事件处理程序附加到元素时加载 DOM。

对我来说,看起来return false你的one回调函数在完成之前正在杀死fadeIn。

您可以将事件对象添加e为函数的参数,然后使用e.stopPropagation()ande.preventDefault()而不是return false;这样:

$('a.login-window').one(function (e) {      
    e.stopPropagation();
    e.preventDefault();
    //Your code   
});
于 2013-04-09T18:26:38.647 回答