2013-03-24 95 views
-1

我有一個jQuery的登錄表單下面的函數,並希望當PHP檢測呼應的是點擊登錄按鈕的JavaScript函數用戶沒有登錄到運行,它會自動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> 
+0

不PHP已經_know_如果用戶登錄在或不? – 2013-03-24 20:27:17

+0

Yikes,請不要將前臺工作與後臺工作混合在一起,例如...請 – 2013-03-24 20:30:20

回答

1
如果你想自動點擊一個按鈕

,您正在尋找.trigger

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

.trigger('event')解釋:

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

(因爲你已經使用jQuery,因此)

1
<?php 
    if (!$lang['LOGIN']) { 
     echo '<script type="text/javascript">'; 
     echo '$(function() {'; 
     echo   '$("a.login-window, a.log").trigger("click")'; 
     echo '});'; 
     echo '</script>'; 
    } 
?> 
1
var loggedIn = isUserLoggedIn(); // check login status 

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

.triggerDocs