开发者

Replace html array notation var with loop value jQuery

开发者 https://www.devze.com 2023-01-17 12:21 出处:网络
I\'m trying to replace the [2] in the name values of the hidden fields with a new value within a loop. The 2 could be an开发者_高级运维y number and so I won\'t necessarily know it when I\'m looping. W

I'm trying to replace the [2] in the name values of the hidden fields with a new value within a loop. The 2 could be an开发者_高级运维y number and so I won't necessarily know it when I'm looping. What would be the best course of action in replacing it?

<input type="hidden" class="pid" name="t[2][p][149][id]" value="ID">
<input type="hidden" class="pane" name="t[2][p][149][name]" value="NAME">


$("input").each(function() {
  name = $(this).attr("name");
  name = name.replace(/^t\[\d+\]/, "t[your number]");
  $(this).attr("name", name);
});


If it will always be the patten "t[number][..." you should create a regular expression and replace it that way.

0

精彩评论

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