开发者

Changing values of children elements

开发者 https://www.devze.com 2022-12-19 03:16 出处:网络
I have parent and child elements structure: <div id=\"container\"> <input type=\"hidden\" id=\"child-1\" value=\"1\" />

I have parent and child elements structure:

<div id="container">
 <input type="hidden" id="child-1" value="1" />
 <input type="hidden" id="child-8" value="1" />
 <input type="hidden" id="child-9" value="1" />
 <input type="hidden" id="child-3" value="1" />
</div> 

I need to select children and change their values.

The result should be:

<div id="container">
 <input type="hidden" id="child-1" value="1" />
 <input type="hidden" id="child-8" value="2" />
 <input type="hidden" id="child-9" value="3" />
 <input type="hidden" id="child-3" value="4" />
</div>开发者_StackOverflow中文版 

JQuery:

$("#container").children().each(function(n){
 $('input[type=hidden]').val(n); 
});

My query code gives no wanted result, because it always changes input hidden value to 4. I know why it makes so, but I can't find any other better solution. So any help would be appreciated.


$("#container input[type=hidden]").each(function(index){
    $(this).val(index + 1);
});

try this

0

精彩评论

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