Jquery sets the disabled element to enabled.

Source: Internet
Author: User

Here we can use three workarounds to change the state of an element that is set to disabled to enabled.
First, change the boolean status of disabled. The code and explanation are as follows: Copy codeThe Code is as follows: $ ("button: eq (2)"). click (function (){
Var text2 = $ ("input: text: eq (2 )");
If (text2.attr ("disabled") = false ){
// Set the disabled boolean attribute to true in the third text input box.
Text2.attr ("disabled", true );
} Else {
// Remove the disabled attribute by setting the disabled attribute of the third text input box to false.
Text2.attr ("disabled", false );
}
});

Method 2: remove the disabled attribute. The code and explanation are as follows:Copy codeThe Code is as follows: $ ("button: eq (1)"). click (function (){
Var text2 = $ ("input: text: eq (1 )");
If (text2.attr ("disabled") = false ){
// Set the value of disabled to disabled in the second text input box.
Text2.attr ("disabled", "disabled ");
} Else {
// Remove the disable attribute of the second text input box.
Text2.removeAttr ("disabled ");
}
});

Third: Change the disabled value. The code and explanation are as follows:Copy codeThe Code is as follows: $ ("button: eq (0)"). click (function (){
Var text1 = $ ("input: text: eq (0 )");
If (text1.attr ("disabled") = ""){
// Or text1.attr ("disabled") = false
// Set the value of disabled to disabled in the first text input box.
Text1.attr ("disabled", "disabled ");
} Else {
// Overwrite the disabled attribute in the first text input box.
Text1.attr ("disabled ","");
}
});

The complete sample code is as follows (tested ):
Copy codeThe Code is as follows: <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head> <script src = "jquery path Introduction"> </script>
<Script type = "text/javascript"> <! --
$ (Document). ready (function (){
$ ("Button: eq (0)"). click (function (){
Var text1 = $ ("input: text: eq (0 )");
If (text1.attr ("disabled") = ""){
// Or text1.attr ("disabled") = false
// Set the value of disabled to disabled in the first text input box.
Text1.attr ("disabled", "disabled ");
} Else {
// Overwrite the disabled attribute in the first text input box.
Text1.attr ("disabled ","");
}
});
$ ("Button: eq (1)"). click (function (){
Var text2 = $ ("input: text: eq (1 )");
If (text2.attr ("disabled") = false ){
// Set the value of disabled to disabled in the second text input box.
Text2.attr ("disabled", "disabled ");
} Else {
// Remove the disable attribute of the second text input box.
Text2.removeAttr ("disabled ");
}
});
$ ("Button: eq (2)"). click (function (){
Var text2 = $ ("input: text: eq (2 )");
If (text2.attr ("disabled") = false ){
// Set the disabled boolean attribute to true in the third text input box.
Text2.attr ("disabled", true );
} Else {
// Remove the disabled attribute by setting the disabled attribute of the third text input box to false.
Text2.attr ("disabled", false );
}
});
});
// --> </Script>
</Head>
<Body>
<Button> disabledNull </button>
<Input type = "text" value = "input something into me! "Size =" 40 "/>
<Br/>
<Button> disabledRemove </button>
<Input type = "text" value = "input something into me! "Size =" 40 "/>
<Br/>
<Button> disabledState </button>
<Input type = "text" value = "input something into me! "Size =" 40 "/>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.