【问题标题】:html5 form submit to onclick js function: nothing is happeninghtml5表单提交到onclick js函数:什么都没有发生
【发布时间】:2015-05-03 11:40:37
【问题描述】:

如果我没有使用正确的术语,请提前道歉。 我得到了一个简单的 sinatra api 来处理用户和组。 我一直在研究这一切的逻辑,虽然一切似乎都在工作,但我无法通过 onclick 调用 issuccess js 函数。我尝试做事件监听器。我现在迷路了,请帮忙。

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>HTML5 Login</title>
    <link rel="stylesheet" href="css/normalize.css">
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <section class="loginform cf">
        <form name="login" method="post" onclick="return isSuccess()" accept-charset="utf-8">
            <ul>
                <li>
                    <label for="usermail">Email</label>
                    <input type="email" name="usermail" placeholder="yourname@email.com" required>
                </li>
                <li>
                    <label for="password">Password</label>
                    <input type="password" name="password" placeholder="password" required></li>
                <li>
                    <input type="submit" value= "login" \>
                </li>
            </ul>
        </form>
    </section>

    <script type="text/javascript">
    function isSuccess(){
        var firstAdmin = {
            "name": "Admin",
            "email": "admin@example.com",
            "password": "secret",
            "admin": true
            };
        $.post("main.rb", firstAdmin);

        function isValidUser() {
            $.get( "/users", function(data) {
                var x=document.form.usermail.value;
                var y=document.form.password.value;
                if (x === users.email && y === users.password && users.admin === true){
                    window.location="userprofile/adminprofile.html";
                }
                if (x === users.email && y === users.password && users.admin === false){
                    window.location="userprofile/userprofile.html";
                }
            alert( "success" );
            })



            });
        }
    }
    </script>
</body>
</html>

【问题讨论】:

    标签: javascript jquery html api sinatra


    【解决方案1】:

    我认为您没有正确使用$get。你查过它的文档吗?

    https://api.jquery.com/jquery.get/

    目前你忽略了 get 的结果:

    if ($.get('/users') = 200){
    

    因为该函数立即返回(无论 Sinatra API 请求是否完成)。您需要传入一个回调函数,该函数会在稍后请求实际完成时立即调用:

    $.get( "/users", function(data) {
      //automatically get here if return status is 200
      alert( "success" );
    })
    

    【讨论】:

    • 感谢您的帮助!! :)
    猜你喜欢
    • 2019-04-16
    • 1970-01-01
    • 2019-01-01
    • 2014-04-13
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多