簡體   English   中英

使用jQuery調用外部函數

[英]Call for External Function using jQuery

我有一個外部js文件,其中包含以下代碼,該文件正在檢查人員登錄憑據是真還是假。 當我將這個jquery放入實際的php文件中時,它將按預期工作,但是當我將jQuery放入js文件並嘗試調用它時,它不起作用,並且我沒有收到任何錯誤。 電話怎么了?

js文件:

function handleLogin(event) {

    // Stop form from submitting normally
    event.preventDefault();

    // Get some values from elements on the page:
    var $form = $( this ),
    username = $form.find( "input[name='username']" ).val(),
    password = $form.find( "input[name='password']" ).val(),
    url = "../process/login.php";

    // Send the data using post
    var posting = $.post( url, { username: username, password: password } );

    // Reset data in #testStatus textarea
    posting.done(function( data ) {
        if (data == '1') {
           $('#loginModal').modal('hide')
           $( "#loginOption" ).replaceWith( "<li id='loginOption'><a href='logout.php'>Logout</a></li>" );
        }
    });
}

php文件(位於php文件末尾的函數調用):

        <!-- Modal -->
    <div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title" id="loginModalLabel">Login</h4>
          </div>
          <div class="modal-body">
            <form id="loginForm" action="">
              <div class="form-group">
                <label for="username">Username</label>
                <input name="username" type="text" class="form-control" id="username" placeholder="Username">
              </div>
              <div class="form-group">
                <label for="password">Password</label>
                <input name="password" type="password" class="form-control" id="password" placeholder="Password">
              </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <input type="submit" id="loginSubmit" name="login" class="btn btn-primary" value="Login">
          </div>
          </form>
        </div>
      </div>
    </div>

<script>
$("#loginForm").submit(function handleLogin() { });
</script>

您需要將函數引用傳遞給Submit事件處理程序...

相反,您正在創建一個對提交處理程序不做任何事情的新函數,因此請嘗試

$("#loginForm").submit(handleLogin);

應該是這樣的:

$("#loginForm").submit(function(e){
  handleLogin(e);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM