This article will introduce to you the solution to jQuery ajax's invalid $ (this). parent (). I hope this method will be helpful to you.
Html
| The Code is as follows: |
Copy code |
<P class = "item">
<Input type = "text" name = "meta_key [164]" value = "file1" size = "20"/> <a href = "/18" id = "164" class = "button remove"> remove </a>
</P> |
Requirement Description:
Click the 'delete' link and delete the page elements based on the ajax return value.
Invalid Method
| The Code is as follows: |
Copy code |
$ ('. Delete'). bind ('click', function (){
$. Ajax ({
Type: 'post ',
Url: $ (this). attr ('href '),
DataType: 'json ',
Data: {id: $ (this). attr ('id ')},
Success: function (msg ){
If (msg. error = 0 ){
Alert (msg. msg );
} Else {
$ (This). parent (). remove (); // The parent element cannot be obtained here
}
}
});
Return false;
}); |
Effective Method
| The Code is as follows: |
Copy code |
$ ('. Delete'). bind ('click', function (){
Div = $ (this). parent (); // obtain the parent element first
$. Ajax ({
Type: 'post ',
Url: $ (this). attr ('href '),
DataType: 'json ',
Data: {id: $ (this). attr ('id ')},
Success: function (msg ){
If (msg. error = 0 ){
Alert (msg. msg );
} Else {
Div. remove (); // delete it again
}
}
});
Return false;
}); |
You can solve other similar problems in the same way.