【问题标题】:Jquery enable/disable button textarea validateJquery 启用/禁用按钮 textarea 验证
【发布时间】:2020-02-01 06:50:42
【问题描述】:

Jsfiddle example

举个例子,我想做这个操作。它不能单独工作。我把它扔到我的服务器上,我运行它时它不起作用错误在哪里?

$( "#validateTxt" ).change(function() {
 if($( "#validateTxt" ).val()=="123"){
 $('#btnCheck').attr("enabled",false);
 }
 else{
  $('#btnCheck').attr("disabled",true);
 }
 
});
<!DOCTYPE html>
<html lang="en" >
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  
  <title></title>
  

</head>
<body>
<!-- partial:index.partial.html -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" integrity="sha384-3ceskX3iaEnIogmQchP8opvBy3Mi7Ce34nWjpBIwVTHfGYWQS9jwHDVRnpKKHJg7" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.3.7/js/tether.min.js" integrity="sha384-XTs3FgkjiBgo8qjEjBk0tGmf3wPrWtA6coPfQDfFEY8AnYJwjalXCiosYRBIBZX8" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js" integrity="sha384-BLiI7JTZm+JWlgKa0M0kGRpJbF2J8q+qreVrKBC47e3K6BW78kGLrCkeRX6I9RoK" crossorigin="anonymous"></script>

////üst kodlar boostrap kütüphanesini çağırıdıgım kodlar projende yoksa ekle 

<input type="text" id="validateTxt" class="form-control" placeholder="123">


<input type="button" class="btn btn-success" value="KontrolEt" id="btnCheck" disabled>
<!-- partial -->
  <script  src="./script.js"></script>
 

</body>
</html>

【问题讨论】:

    标签: javascript jquery html validation textarea


    【解决方案1】:

    你可以这样做

    $('#btnCheck').attr("disabled", false);
    

    html没有enabled属性,所以disabled属性需要设置为false

    $("#validateTxt").change(function() {
      if ($("#validateTxt").val() == "123") {
        $('#btnCheck').attr("disabled", false);
      } else {
        $('#btnCheck').attr("disabled", true);
      }
    
    });
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
      <title></title>
    
    
    </head>
    
    <body>
      <!-- partial:index.partial.html -->
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" integrity="sha384-3ceskX3iaEnIogmQchP8opvBy3Mi7Ce34nWjpBIwVTHfGYWQS9jwHDVRnpKKHJg7" crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.3.7/js/tether.min.js" integrity="sha384-XTs3FgkjiBgo8qjEjBk0tGmf3wPrWtA6coPfQDfFEY8AnYJwjalXCiosYRBIBZX8" crossorigin="anonymous"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js" integrity="sha384-BLiI7JTZm+JWlgKa0M0kGRpJbF2J8q+qreVrKBC47e3K6BW78kGLrCkeRX6I9RoK" crossorigin="anonymous"></script>
    
      ////üst kodlar boostrap kütüphanesini çağırıdıgım kodlar projende yoksa ekle
    
      <input type="text" id="validateTxt" class="form-control" placeholder="123">
    
    
      <input type="button" class="btn btn-success" value="KontrolEt" id="btnCheck" disabled>
      <!-- partial -->
      <script src="./script.js"></script>
    
    
    </body>
    
    </html>

    【讨论】:

      【解决方案2】:

      没有启用attribute for the &lt;input&gt; element

      如果条件为true,则可以将disabled 设置为false,否则使用.removeAttr() 删除属性。

      另外我更喜欢使用input 事件而不是change

      $("#validateTxt").on('input', function() {
       if($("#validateTxt").val()=="123"){
         $('#btnCheck').attr("disabled", true);
       }
       else{
         $('#btnCheck').removeAttr("disabled");
       }
      });
      <!-- partial:index.partial.html -->
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" integrity="sha384-3ceskX3iaEnIogmQchP8opvBy3Mi7Ce34nWjpBIwVTHfGYWQS9jwHDVRnpKKHJg7" crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.3.7/js/tether.min.js" integrity="sha384-XTs3FgkjiBgo8qjEjBk0tGmf3wPrWtA6coPfQDfFEY8AnYJwjalXCiosYRBIBZX8" crossorigin="anonymous"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js" integrity="sha384-BLiI7JTZm+JWlgKa0M0kGRpJbF2J8q+qreVrKBC47e3K6BW78kGLrCkeRX6I9RoK" crossorigin="anonymous"></script>
      
      
      <input type="text" id="validateTxt" class="form-control" placeholder="123">
      
      <input type="button" class="btn btn-success" value="KontrolEt" id="btnCheck" disabled>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-03-12
        • 2017-11-07
        • 1970-01-01
        • 1970-01-01
        • 2011-02-02
        • 2017-07-30
        • 1970-01-01
        相关资源
        最近更新 更多