开发者

Changing input name attribute

开发者 https://www.devze.com 2022-12-26 14:59 出处:网络
I have some hidden inputs like this <input name=\"exam.normals[1].blahblah\" ..../> I would like somehow to replace the [1] with a number that I want (index).

I have some hidden inputs like this

<input name="exam.normals[1].blahblah" ..../>

I would like somehow to replace the [1] with a number that I want (index).

I aint lazy but I am trying to find a good way to do thi开发者_开发技巧s...

A solution would be a replace of exam.normals[1] with exam.normals[+ index +] but I should substr the whole string first.... With regexp I don’t know how to do the replace. good...


You can do this:

$("input[name*='[1]']").attr('name', function(i, v) {
  return v.replace('[1]', '[' + i + ']');
});

This finds inputs that have a name with [1] in them, then replaces [1] with [0] on the first [1] on the second, etc.


Try something like this:

$("input[name]").each(function() {
    var $this = $(this);
    $this.attr("name", $this.attr("name").match(/\[\d+]/g, "["+index+"]"));
});
0

精彩评论

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

关注公众号