Judgment method: 1. Use "the element that needs to be judged.attr("disabled");" to obtain the disabled attribute value of the element; 2. Use "if(to obtain the disabled attribute value of the element == "disabled" ){The element has the code for this attribute;}" to make a judgment.
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
1. Use the attr method to obtain the disabled attribute of an attribute
attr() method sets or returns the attribute Select the attributes and values of the element.
When this method is used to return an attribute value, the value of the first matching element is returned.
When this method is used to set attribute values, one or more attribute/value pairs are set for the matching element.
Syntax
Return the value of the attribute:
$(selector).attr(attribute)
2. Use if loop to make judgments
if 语句 if(条件表达式){ 语句... }
if statement is executed , the conditional expression will be evaluated first.
If the value of the conditional expression is true, the statement after the if will be executed.
If the value of the conditional expression is false, then The statement after if will not be executed
The if statement can only control the statement that follows it
If you want the if statement to control multiple statements, you can put these statements into a code block The code block after the {}
if statement is not necessary, but try to write a code block during development, even if there is only one statement after the if.
The example is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>123</title> <script src="js/jquery.min.js"> </script> <script> $(document).ready(function(){ var res=$("#mybutton").attr("disabled"); if(res=="disabled"){ alert('该元素有disabled属性'); } }); </script> </head> <body> <button id="mybutton" type="button" disabled>有disabled属性的元素</button> </body> </html>
Output result:
##Related Video tutorial recommendation:The above is the detailed content of How does jquery determine whether an element has a disabled attribute?. For more information, please follow other related articles on the PHP Chinese website!