jQuery 禁用或启用一个输入元素

jQuery 禁用或启用一个输入元素

在jQuery中禁用/启用一个输入元素可以通过使用prop()方法完成。prop()方法是用来设置或返回所选元素的属性和值。

示例1:本例使用prop()方法来禁用输入文本字段。

<!DOCTYPE html>  
<html>  
  
<head> 
    <title> 
        JavaScript enable/disable
        an input element
    </title>
      
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
</head> 
          
<body style = "text-align:center;">  
  
    <h1 style = "color:green;" >  
        GeeksForGeeks  
    </h1>  
              
    <input id = "input" type="text" name="input"/> 
          
    <button onclick="enable_disable()"> 
        Enable/Disable
    </button> 
          
    <!-- Script to disable input text area -->
    <script> 
        function enable_disable() {
            $("input").prop('disabled', true);
        }
    </script> 
</body>  
  
</html>

输出:

  • 在点击按钮之前。
    jQuery 禁用/启用一个输入元素
  • 点击该按钮后。
    jQuery 禁用/启用一个输入元素

示例2:本例使用prop()方法启用输入文本字段。

<!DOCTYPE html>  
<html>  
  
<head> 
    <title> 
        JavaScript enable/disable
        an input element
    </title>
      
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
</head> 
          
<body style = "text-align:center;">  
  
    <h1 style = "color:green;" >  
        GeeksForGeeks  
    </h1> 
      
    <input id = "input" type="text" name="input" disabled/> 
      
    <button onclick="enable_disable()"> 
        Enable/Disable
    </button> 
      
    <!-- Script to enable input text fields -->       
    <script> 
        function enable_disable() {
            $("input").prop('disabled', false);
        }
    </script> 
</body>  
  
</html>

输出:

  • 在点击按钮之前。
    jQuery 禁用/启用一个输入元素
  • 点击该按钮后。
    jQuery 禁用/启用一个输入元素

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程