表单确认JQuery .submit()

时间:2012-12-24 05:18:58

标签: jquery forms jquery-mobile submit

我有一个表单需要使用摘要式身份验证进行验证。我还在进行身份验证,但我已经制定了基本的HTML表单。

<form method="post" id="loginForm" class="validate">

            <div data-role="fieldcontain">
                <label for="password">Email:</label> 
                <input class="required email" type="text" name="username" id="username" placeholder="username@target.com">
            </div>

            <div data-role="fieldcontain"> 
                <label for="password">Password:</label>
                <input class="required" type="password" name="password" id="password" placeholder="password">
            </div>

            <input type="submit" value="Login">

        </form>

我还有一个名为digest_auth.js的外部js文件,其方法为

$('#loginForm').submit(function() {
  alert('click');   
  username = $('#username').value();
  password = $('#password').value();
  realm = tokens['realm'];
  nonce = tokens['nonce'];
  qop = tokens['qop'];
  alert('submitted');
  ha1 = bcrypt(username + ":" + realm + ":" + password);
  ha2 = bcrypt("GET:" + "http://localhost:8090/web/login");
  response = bcrypt(ha1 + ":" + nonce + ":" + nc + ":" + cnonce + ":" + qop + ":" ha2);

  $.ajax(
    type: 'GET', // maybe POST?
    url: url,
    complete: function(xhr, status) {
      if (status == 200) {
        // success. save the nonce and nc in the local storage.
        // whenever you send a request to the server, use the nonce
        // and nc
          alert('Success');
      }
      else {
        // failure, try again
          alert('Failure');
      }
    }
  )
});

但是,没有调用.submit。我在开头添加了alert(),但什么都没得到。我用

链接了外部
<script type="text/javascript" src="digest_auth.js"></script>

2 个答案:

答案 0 :(得分:0)

问题在于你的ajax请求:

错误:

  1. “+”在响应串联中缺失
  2. ajax属性应该在大括号内
  3. 使用此:

    $(document).ready(function(){
      $('#loginForm').submit(function() {
          alert('click');   
          username = $('#username').value();
          password = $('#password').value();
          realm = tokens['realm'];
          nonce = tokens['nonce'];
          qop = tokens['qop'];
          alert('submitted');
          ha1 = bcrypt(username + ":" + realm + ":" + password);
          ha2 = bcrypt("GET:" + "http://localhost:8090/web/login");
          response = bcrypt(ha1 + ":" + nonce + ":" + nc + ":" + cnonce + ":" + qop + ":" + ha2);
    
          $.ajax({
            type: 'GET', // maybe POST?
            url: url,
            complete: function(xhr, status) {
              if (status == 200) {
                // success. save the nonce and nc in the local storage.
                // whenever you send a request to the server, use the nonce
                // and nc
                  alert('Success');
              }
              else {
                // failure, try again
                  alert('Failure');
              }
            }
          })
        });
    });
    

答案 1 :(得分:0)

确保在页面中存在表单元素后执行$('#loginForm')

如果该脚本运行并且找不到任何形式,则提交将触发,但不会触发您的功能。

它在这里工作:http://jsfiddle.net/MxgEf/7/