Element in a form
How to check if an html element [te开发者_Python百科xtbox, select, radiobutton etc] is in a form via Jquery?
You can use closest
to see if there is an ancestor element of the type form
:
obj.closest("form").length == 1
You could also do
obj.is('form *')
If you want to see if the object be in a particular form, you could do this:'
obj.is('#formId *')
Try this:
<script type="text/javascript">
function checkboxInForm( form ){
return ( form.find(':checkbox').size() ) ? true : false;
}
$(document).ready(function() {
var myForm = $('#myFormId');
if ( checkboxInForm( myForm ) ){
// do something
}
});
</script>
Create a function for each kind of element you need to find.
精彩评论