【问题标题】:Form Confirmation JQuery .submit()表单确认 JQuery .submit()
【发布时间】:2012-12-24 05:18:57
【问题描述】:

我有一个需要使用摘要式身份验证来验证的表单。我仍在进行身份验证,但我已经布置了基本的 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>

【问题讨论】:

  • 你的 javascript 是在 DOM 之后加载的吗(是 body 标签中的脚本)?如果没有,请将其放入$(function() { ... your code here.. }); jquery closuer(onready 函数)
  • 你是如何为 ajax 创建“url”的?

标签: jquery forms jquery-mobile submit


【解决方案1】:

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

如果该脚本运行并没有找到表单,则提交将触发但不会触发您的函数。

在这里有效:http://jsfiddle.net/MxgEf/7/​​​

【讨论】:

  • 对不起,表单存在后。如果该脚本运行并且找不到表单,则提交将触发但不会触发您的函数。
  • 所以我的 HTML 文档中是否需要其中之一 $(document).ready(function() { }); ?
  • 这里适合你吗? jsfiddle.net/MxgEf/7如果你在这个例子中添加更多代码,我可以看看它什么时候坏了。
  • 是的,这是最令人沮丧的部分。它在 jsfiddle 中工作得很好,但在我的电脑上不起作用!!!
【解决方案2】:

问题在于您的 ajax 请求:

错误:

  1. 响应连接中缺少“+”
  2. ajax 属性应该在大括号内

使用这个:

$(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');
          }
        }
      })
    });
});

【讨论】:

  • 内联会起作用,但这会不必要地将他的脚本与 HTML 结合起来。他只是在元素进入 DOM 之前选择元素(因为脚本可能在他的&lt;head&gt; 中,而不是在onready 之后)。
  • 我已更新
    属性,请检查是否显示该警报。
猜你喜欢
  • 2017-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-25
  • 1970-01-01
  • 2012-02-13
  • 1970-01-01
相关资源
最近更新 更多