-1

我有以下用于 jquery 登录表单的函数,并希望在 php 通过回显自动单击登录按钮的 javascript 函数检测到用户未登录时运行它

jq代码:

<script type="text/javascript">

             $(document).ready(function() {
$('a.login-window, a.log').click(function() {

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

    //Fade in the Popup
    $(loginBox).fadeIn(300);

    //Set the center alignment padding + border see css style
    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;
});
});
             </script>

HTML

 <div id="login-box" class="login-popup">
    <a href="#" class="close"><img src="close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>
      <form method="post" method="post" name="f" id="f" class="signin" action="login.php">
            <fieldset class="textbox">
            <label class="username">
            <span>Username or email</span>
            <input id="username" name="username" value="" type="text" autocomplete="on" placeholder="Username">
            </label>
            <label class="password">
            <span>Password</span>
            <input id="password" name="password" value="" type="password" placeholder="Password">
            </label>
<button class="submit button" type="submit" name="Submit" value="<?php echo $lang['LOGIN'];" style="background:none;">Sign in</button>
            <p>
            <a class="forgot" href="#">Forgot your password?</a>
            </p>        
            </fieldset>
      </form>
</div>
4

3 回答 3

1
var loggedIn = isUserLoggedIn(); // check login status

if(!loggedIn){
  $('a.login-window, a.log').trigger('click');
}

.trigger 文档

于 2013-03-24T20:34:15.500 回答
1

如果你想自动点击一个按钮,你正在寻找.trigger

$('#some-id').trigger('click');

.trigger( 'event' )解释:

Execute all handlers and behaviors attached to the matched elements 
for the given event type.

(因为您已经在使用 jQuery,因此)

于 2013-03-24T20:28:48.940 回答
1
<?php
   if (!$lang['LOGIN']) {
       echo '<script type="text/javascript">';
       echo    '$(function() {';
       echo         '$("a.login-window, a.log").trigger("click")';
       echo    '});';
       echo '</script>';
   }
?>
于 2013-03-24T20:29:12.240 回答