开发者

Element in a form

开发者 https://www.devze.com 2022-12-24 15:55 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消