从HTML表单事件中调用解析javascript函数

时间:2012-12-27 19:36:03

标签: javascript html dbaas

我一直试图从html表单提交中调用一个简单的javascript登录函数,它似乎没有调用该方法。我已经尝试将该方法移动到外部.js类,该函数仍然无法正常工作。 java脚本函数位于名为Login()的头文件中,它使用表单中的变量将它们传递给Web服务并登录。我也试过从外部homepage.js脚本调用其他函数,他们没有触发事件。如果登录成功,我的目的是推送到index.html页面。感谢。

代码:

<script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.1.15.min.js"></script>     

    <script type="text/javascript">
    Parse.initialize("v9s5EdsZ4u9fSfTpr8SD0u3Xcb56nen1GWge47kV", "fjuiNrsJk3AubBY1zDfosLKDoPxzGgKlUxegxbtx");
function Login()
    {
      Parse.User.logIn(document.loginForm.uname, document.loginForm.pword, 
      {
        success: function(user) 
        {
          window.location="/index.html";
        },
        error: function(user, error) 
        {
          // The login failed. Check error to see why.
        }
      });
    }
  </script>

    <style type="text/css">
      /* Override some defaults */
      html, body {
        background-color: #eee;
      }
      body {
        padding-top: 40px; 
        padding-bottom: 140px;
      }
      .container {
        width: 300px;
      }

      /* The white background content wrapper */
      .container > .content 
      {
        background-color: #fff;
        padding: 20px;
        margin: 0 -20px; 
        -webkit-border-radius: 10px 10px 10px 10px;
           -moz-border-radius: 10px 10px 10px 10px;
                border-radius: 10px 10px 10px 10px;
        -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15);
           -moz-box-shadow: 0 1px 2px rgba(0,0,0,.15);
                box-shadow: 0 1px 2px rgba(0,0,0,.15);
      }

    .login-form {
    margin-left: 65px;
    }

    legend {
    margin-right: -50px;
    font-weight: bold;
      color: #404040;
    }

    </style>

  </head>
  <body>
    <div id="topBar">
      <button onclick="WarningSigns()" class="btn btn-danger" type="button">Warning signs</button>
    </div>

    <div id="content">
      <div id="titleContent">
        <h1>Bronchiectasis</h1>
        <p id="subTitle">Self-management action plan <p>
      </div>
    </div>
      <div class="container">
    <div class="content">
      <div class="row">
        <div class="login-form">
          <h2>Login</h2>
          <form name="loginForm" action="javascript:Login()">
            <fieldset>
              <div class="clearfix">
                <input name="uname" type="text" placeholder="Username">
              </div>
              <div class="clearfix">
                <input name="pword" type="password" placeholder="Password">
              </div>
              <button class="btn primary" type="submit">Sign in</button>
            </fieldset>
          </form>
        </div>
      </div>
    </div>
  </div> <!-- /container -->



  </body>

1 个答案:

答案 0 :(得分:2)

logIn function from the Parse API需要其用户名和密码参数的字符串值。您正在传递HTML元素。

将通话更改为:

  Parse.User.logIn(document.loginForm.uname.value, document.loginForm.pword.value, 
  {
    success: function(user) 
    {
      window.location="/index.html";
    },
    error: function(user, error) 
    {
      alert('error '+error);
      // The login failed. Check error to see why.
    }
  });

你应该更进一步。

更新小提琴:http://jsfiddle.net/JYsZe/1/