开发者

When using :input getting attribute value of input in question/context

开发者 https://www.devze.com 2023-03-23 06:50 出处:网络
I am trying this var newNum = some new number; $(\"#myDiv fieldset:last\").find(\':input\').attr(\'name\', $(this).attr(\'name\') + newNum);

I am trying this

var newNum = some new number;

$("#myDiv fieldset:last").find(':input').attr('name', $(this).attr('name') + newNum);

If an input field in myDiv is 'firstName' i want to rename it 'firstName15'开发者_JAVA技巧 may be!

How can i achieve this?

Thanks.


Try using .each()

$("#myDiv fieldset:last").find(':input').each(function(i, e) { 
    $(e).attr('name', $(e).attr('name') + newNum);
});

Of course if you are going to be incrementing the counter in the operation make sure you account for that, but if you are only doing one assignment, you can create a variable containing the element you are selecting and operate on that otherwise $this in the context above does not refer the element you are selecting.


var $input = $("#myDiv fieldset:last").find(':input');

$input.attr('name', $input.attr('name') + newNum);

this is not the input element in that context.

EDIT: I may have misunderstood your question slightly. If your intent is to work on multiple input elements and not just one, use @Quintin's solution with each.

0

精彩评论

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

关注公众号