2017-01-01 63 views
-2

我試圖禁用表單上的提交按鈕時,用戶名,電子郵件或密碼字段或全部是空的,用戶填寫所有的然後我啓用提交按鈕。但下面的代碼並不會禁用提交按鈕。爲什麼下面的jquery代碼禁用提交按鈕?

感謝

HTML代碼:

<form id="new_user_form"> 
             {% csrf_token %} 
            <div class="modal-content" data-method="post" id="modalform" > 
              <div class="modal-header"> 
               <button type="button" class="close" data-dismiss="modal">&times;</button> 
               <h4 class="modal-title">Sign Up</h4> 
              </div> 
             <div onload="validateForm();" class="modal-body"> 
              <div class="form-group"> 
               <label for="usr">Username:</label> 
               <input type="text" name="username" class="form-control input-sm" id="username"> 
               <label for="eml">Email:</label> 
               <input type="email" name="emil" class="form-control input-sm" id="emailid"> 
               <label for="passwordd">Password:</label> 
               <input onchange="checkPasswordMatch();" type="password" name="password" class="form-control input-sm" id="password"> 
               <label for="passwordd">Retype Password:</label> 
               <input type="password" name="retrypassword" class="form-control input-sm" id="retrypassword"> 
               <br/><div style="color:blue;" id="doesPasswordMatch"> </div> 
               <span class="help-block">Ensure, You Choose Correct Profession...</span> 

             <label for="inputfile">Upload Profile Picture</label> 
             <input type="file" id="inputfile"> 
             <p class="help-block">The Profile Picture help people to identify you.</p> 
              </div role="Form group"> 
              </div role="modal-dialog"> 
         <!--Closing Of Sign Up Modal Page --> 
               <div class="modal-footer"> 
                   <button type="submit" class="btn btn-default" id="submit" onclose="showSuccessMessage();">Submit</button> 
                   <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
               </div role="modal-footer"> 
            </div role="modal content"> 

jQuery代碼:

<script type="text/javascript"> 
    function checkPasswordMatch() { 
     if ($('#password').val() == $('#retrypassword').val()) 
      $('#doesPasswordMatch').html("<h4> Match </h4>").css('color', 'rgb(34,139,34)'); 
     else 
      $('#doesPasswordMatch').html("<h4> Does not match </h4>").css('color', 'rgb(128,0,0)'); 

    } 
    $(document).ready(function() { 
     $('#submit').attr('disable', true); 
     $('#retrypassword').keyup(checkPasswordMatch); 
     if ($('#username').val() == '' || $('#email').val() == '' || $('#password').val() == '') 
      $('#submit').attr('disable', false); 
    }); 
</script> 
+0

你應該我們e'prop'而不是'attr' – Musa

+0

@Musa:確實如此,儘管'attr'可以工作,因爲jQuery會爲您修復它。 –

+0

,它的禁用不禁用 – sheavens

回答

3

該屬性是disabled(具有d末),不disable。雖然attr將您處理該問題由於歷史原因,設置了正確的方法是通過prop

$("#submit").prop("disabled", true); 
// ----------^^^^---------^ 
+0

它仍然不起作用,這是什麼downvoting ??? – Ahtisham

+0

@Ahtisham:如果它仍然不起作用,那麼代碼有另一個*問題。這是跳出來的明顯的一個(好的,兩個)。使用你的調試器來遍歷代碼,檢查變量等等。 –

1

應該disabled和NOT disable

這應該工作:

$('#submit').attr('disabled',true); 

$('#submit').attr("disabled","disabled"); 
+0

它仍然不起作用,這是什麼downvoting ??? – Ahtisham

+0

我沒有downvote你的問題..其他人可能有。 –

+0

好吧布羅達有去我的投票給你:)但它仍然無效:( – Ahtisham

相關問題