jquery how to get form element types, names and values

前端 未结 5 1629
失恋的感觉
失恋的感觉 2020-12-30 07:34

I know I can get the name/value relationship by using

$(#form).serializeArray();

But is there a way to get the whole enchilada, type, name

5条回答
  •  攒了一身酷
    2020-12-30 08:20

    The below code helps to get the details of elements from the specific form with the form id,

    $('#formId input, #formId select').each(
        function(index){  
            var input = $(this);
            alert('Type: ' + input.attr('type') + 'Name: ' + input.attr('name') + 'Value: ' + input.val());
        }
    ):
    

    The below code helps to get the details of elements from all the forms which are place in the loading page,

    $('form input, form select').each(
        function(index){  
            var input = $(this);
            alert('Type: ' + input.attr('type') + 'Name: ' + input.attr('name') + 'Value: ' + input.val());
        }
    ):
    

    The below code helps to get the details of elements which are place in the loading page even when the element is not place inside the tag,

    $('input, select').each(
        function(index){  
            var input = $(this);
            alert('Type: ' + input.attr('type') + 'Name: ' + input.attr('name') + 'Value: ' + input.val());
        }
    ):
    

    NOTE: We add the more element tag name what we need in the object list like as below,

    Example: to get name of attribute "fieldset",
    
    $('input, select, fieldset').each(
        function(index){  
            var input = $(this);
            alert('Type: ' + input.attr('type') + 'Name: ' + input.attr('name') + 'Value: ' + input.val());
        }
    ):
    

提交回复
热议问题