admin管理员组

文章数量:1302328

I'm tryng to get all the ID,value of each input element on 'page-templete1' and i didn't succeed

function Temp1() {          

var input = [];

$('page-templete1 input[name][id][value]').each(function(){


input.push(this);
cc=$(this).attr('id'));
 alert(cc); // trying to get one at time (not working)});
alert(input[0].id); // not working

alert(input[0].attr('id')); // not working

});

alert(input[0].id); // not working

alert(input[0].attr('id')); // not working 

}

how can I get ID,value of all Input elements in a page and have access one them later note: I done know the IDs of the input elements or how many. there is an old post talking about similar problem but didn't solve my problem

I'm tryng to get all the ID,value of each input element on 'page-templete1' and i didn't succeed

function Temp1() {          

var input = [];

$('page-templete1 input[name][id][value]').each(function(){


input.push(this);
cc=$(this).attr('id'));
 alert(cc); // trying to get one at time (not working)});
alert(input[0].id); // not working

alert(input[0].attr('id')); // not working

});

alert(input[0].id); // not working

alert(input[0].attr('id')); // not working 

}

how can I get ID,value of all Input elements in a page and have access one them later note: I done know the IDs of the input elements or how many. there is an old post talking about similar problem but didn't solve my problem

Share Improve this question edited Nov 16, 2017 at 7:45 Diaa Saada asked Feb 21, 2013 at 12:14 Diaa SaadaDiaa Saada 1,0561 gold badge12 silver badges22 bronze badges 1
  • try removing the [name][id][value]at from the selector – Hugo Alves Commented Feb 21, 2013 at 12:17
Add a ment  | 

6 Answers 6

Reset to default 5

Depending on whether your "page" is an id you may need the dot or hash before the selector:

var results = [];
$('#page-templete1 input').each(function(){
    results.push({
        id: this.id,
        value: this.value
    });
});

Try the below code :

  $(function(){
    $("input").each(function(){
        alert($(this).attr('id'))
    })
    })
var inputs = [];
$('page-templete1 input').each(function(){
    var id=$(this).attr('id'),
        val = $(this).val();
    alert("id: " + id + " with value: "+ val);
    inputs.push({id:id, value:val});
});

You are assigning an array to a variable: var inputs = []; and then try to read the array by referencing an undefined variable: input.push(this); (using input instead of inputs).

Try to use jquery input selector and iterate over it.

Try something like this

        $(function(){
             jQuery( "#pageTemplate :input" ).each(function(){
                var id = this.id;       // for id
                var value = this.value; // for value
            })
        })

Note : you have to specify whether your page-templete1 is id or class

serializeArray will help you try this:

var array = jQuery('your input selector').serializeArray();

the result will be

array{{name="", value=""})

to access try this

var array = jQuery('input').serializeArray();
array.each(function(obj){
    //do what with your object
});

本文标签: javascriptJQuery get IDValue of all Input elementsStack Overflow