0
$('#.login.button').click(function() {

 $('#.register.button').click(function() {

这两个都做事情,我将如何在单击或输入新闻时运行它们下的脚本?

这是我的 .js jQuery:

$(document).ready(function() {
    $('#.login.button').click(function() {

        // Getting the variable's value from a link 
        var loginBox = $(this).attr('href');

        //Fade in the Popup and add close button
        $(loginBox).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;
    });
    $("#.sign-in.button").click(function() {
    var empty = $(this).parent().find("input").filter(function() {
        return this.value === "";
    });
    if(empty.length) {
        var notification = noty({text: "Error: Some fields were not filled out" , type:"error",  timeout: 2500, layout:"topCenter"});
        return false;
    }
 var username = $('#username').val(),
 password = $('#password').val();
$.ajax({
    url: "components/login.php",
    data:{username:username, password:password},
    type: "post",
    success: function(data) {
                    if(data == 'success') {
                        var notification = noty({text: "Welcome, "+username+"!" , type:"success",  timeout: 2500, layout:"topCenter"});
                        setTimeout(function(){
                         window.location = document.URL;
                    }, 3000);}
                    else {
                    var notification = noty({text: "Error: Incorrect Username/Password" , type:"error",  timeout: 2500, layout:"topCenter"});
                    }
    },
    error: function(html) {
    alert('error');
},
    statusCode: {
     404: function() {
       //404 here
     }
    }

});
return false;
});

$('#.register.button').click(function() {
                var registerBox = $(this).attr('href');
                $(registerBox).fadeIn(300);

        //Set the center alignment padding + border
        var popMargTop = ($(registerBox).height() + 24) / 2; 
        var popMargLeft = ($(registerBox).width() + 24) / 2; 

        $(registerBox).css({ 
            'margin-top' : -popMargTop,
            'margin-left' : -popMargLeft
        });

        // Add the mask to body
        $('body').append('<div id="mask"></div>');
        $('#mask').fadeIn(300);
            return false;
    });

$("#.log-out.button").click(function() {
$.ajax({
    url: "components/logout.php",
    success: function(data) {
                        var notification = noty({text: "Successfully logged out!" , type:"success",  timeout: 2500, layout:"topCenter"});
                        setTimeout(function(){
                         window.location = document.URL;
                    }, 3000);
    },
    error: function(html) {
    alert('error');
},
    statusCode: {
     404: function() {
       //404 here
     }
    }

});
return false;
});

});
4

2 回答 2

0

用这个:

$("#password").keydown(function(event){
    if(event.keyCode == 13) {
        $('#loginbtn').click();
    };
});

然后制作你的按钮:

<button id="loginbtn" type="button">Sign in</button>
于 2013-02-08T02:49:44.837 回答
-1

第一要务,$('#.login.button')行不通。

$('#loginbutton')

<button id="loginbutton">Log in</button>

将要。

完成后,您应该始终将事件绑定到表单提交,因为提交表单的方法不止一种。

$('#theform').on('submit', function(){
    // do something useful here
});

假设它的形式是按钮提交它。您可以让它以前做一些事情,假设您有一个使用相同电子邮件和密码字段的注册和登录按钮。

$('#loginbutton').on('click', function(){
    // do the unique thing here
    // then let the form submit
    return true;
});    

或者如果出于某种原因,此按钮不在表单中

$('#loginbutton').on('click', function(){
    // do the unique thing here
    // then let the form submit
    $('#theform').trigger('submit');
});

我想这就是你要问的。

于 2013-02-08T01:53:02.073 回答