简体   繁体   English

使用jQuery调用外部函数

[英]Call for External Function using jQuery

I've got an external js file with the following code in it that is checking to see if a person login credentials are true or false. 我有一个外部js文件,其中包含以下代码,该文件正在检查人员登录凭据是真还是假。 When I put this jquery into the actual php file it will work as intended but when it I put the jQuery into the js file and try calling it it doesn't work and I'm not getting any errors. 当我将这个jquery放入实际的php文件中时,它将按预期工作,但是当我将jQuery放入js文件并尝试调用它时,它不起作用,并且我没有收到任何错误。 What's wrong with the call? 电话怎么了?

js file: 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 file (the call of the function located at the end of the php file): 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>

You need to pass the function reference to the submit event handler... 您需要将函数引用传递给Submit事件处理程序...

Instead you are creating a new function which does not do anything to the submit handler, so try 相反,您正在创建一个对提交处理程序不做任何事情的新函数,因此请尝试

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

It should be like this: 应该是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM