【问题标题】:disable form submit button when password does not match密码不匹配时禁用表单提交按钮
【发布时间】:2013-02-23 07:41:58
【问题描述】:

我试图找出我应该在哪里包含一个 js 代码,当密码不匹配时,我会在其中禁用表单提交按钮。

html:

<form name=passform class="form-horizontal-signup" method="post" action="login.php" >



    <fieldset>
    <legend>Sign Up for MyVibes</legend>
    <input type="password" class="input-xlarge" placeholder="Password" name="password" id="password" onKeyUp="verify.check()" />
    <input type="password" class="input-xlarge" placeholder="Retype Password" name="repassword" id="repassword" onKeyUp="verify.check()"/></fieldset>
    <div id="password_result">&nbsp; </div><br />
    <button type="submit" input name="btnSignUp" id="btnSignUp" class="btn btn-inverse" disabled="disabled"/>Sign Up</button>
    </form>
    </div>

获取变量:

<SCRIPT TYPE="text/javascript">


            verify = new verifynotify();
            verify.field1 = document.passform.password;
            verify.field2 = document.passform.repassword;
            verify.result_id = "password_result";
            verify.match_html = "Passwords match.";
            verify.nomatch_html = "Please make sure your passwords match.";

            // Update the result message
            verify.check();

            //
            </SCRIPT>

和功能:

function verifynotify(field1, field2, result_id, match_html, nomatch_html) {
 this.field1 = field1;
 this.field2 = field2;
 this.result_id = result_id;
 this.match_html = match_html;
 this.nomatch_html = nomatch_html;

 this.check = function() {

   // Make sure we don't cause an error
   // for browsers that do not support getElementById
   if (!this.result_id) { return false; }
   if (!document.getElementById){ return false; }
   r = document.getElementById(this.result_id);
   if (!r){ return false; }

   if (this.field1.value != "" && this.field1.value == this.field2.value) {
     r.innerHTML = this.match_html;
      $("#btnSignUp").prop("disabled", false);

   } else {
     r.innerHTML = this.nomatch_html;
      $("#btnSignUp").prop("disabled", true);

   }
 }
}

也许我放错地方了 $("#btnSignUp").prop("disabled", true);代码?

【问题讨论】:

  • 既然你用的是jQuery,为什么不支持getElementById的浏览器需要自己保护呢?

标签: php javascript


【解决方案1】:

试试

$("#btnSignUp").attr("disabled", "disabled");

【讨论】:

    【解决方案2】:

    这应该有效..

    if (this.field1.value != "" && this.field1.value == this.field2.value) {
     r.innerHTML = this.match_html;
      $("#btnSignUp").removeAttr("disabled");
    
    } else {
     r.innerHTML = this.nomatch_html;
      $("#btnSignUp").attr("disabled", "disabled");
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-30
      • 2016-04-03
      • 1970-01-01
      • 2010-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多