开发者

Looping through hidden fields

I was wondering how i would loop through hidd开发者_开发百科en fields with jquery to get their values. The hidden fields are in a php while loop.


$('input:hidden').each(function() {
    var value = $(this).val();
    // do something with the value
});


It doesn't mather the hidden fields are created in a php loop.

You can loop like this:

$("input[type='hidden']").each(function() {
    alert($(this).val());
});


try this ..

$('input[type=hidden]').each(function(){
 var hiddenValue = $(this).val();  
});


$(document).ready(function ()
{
  $('input[type=hidden]').each(function()
  {
    var currentValue = $(this).val();
  });
});

This will make sure the loop is being made only after the document is ready (assuming you don't trigger the loop with a click. You can also do:

function LoopingThrough()
{
  $('input[type=hidden]').each(function()
  {
    var currentValue = $(this).val();
  });
}

In case no you don't understand the different between .val() and .attr('value') it's this: when doing .attr('value') jQuery searches for the 'value' attribute in the element. If you're going through a drop down list, the element wont have a value attribute. .val() on the other hand already knows how to handle elements that has a value, but don't have the 'value' attribute.

My Development Blog


as simply as:

$("input[type='hidden']").each(function(index){
    //Work here with this.
})

or you can just use the pseudo selector as stated by several other's

$(":hidden").each(function(index){
    //Work here with this.
})

but the above would also find any element that has display:none regardless of its type, some other factors of using :hidden are:

  • They have a CSS display value of none.
  • They are form elements with type="hidden".
  • Their width and height are explicitly set to 0.
  • An ancestor element is hidden, so the element is not shown on the page.

if you wish to get the value of the input then you can just use

var Value = $(this).attr("value");

or

var Value = $(this).val();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜