jQuery每个循环重命名一个ID的每个实例 [英] jQuery each loop to rename every instance of an ID

查看:45
本文介绍了jQuery每个循环重命名一个ID的每个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面在一个表中创建动态创建的行,其输入ID为fixedRate

I have a page that is creating dynamically created rows in a table with an input with an ID "fixedRate"

我正在尝试重命名fixedRate的每个实例ID。这仅适用于我当前代码的id的第一个实例。

I am trying to rename each instance of the fixedRate id. This is only working for the first instance of the id with my current code.

以下是代码:

var amountRows = $("#billTasks > tr").size();
var i=1;


$("#fixedRate").each(function(){

if (i == amountRows) {
    return false; 
}
    this.id = this.id + i;
    i++;
});

我认为代码应该工作的方式是添加1,2,3,... fixedRate ID的每个实例的单词fixedRate的结尾。 (即fixedRate1,fixedRate2,...)

The way I think the code should work is adding a 1,2,3,... at the end of the word fixedRate for each instance of the fixedRate ID. (i.e. fixedRate1, fixedRate2,...)

我是否需要使用while循环?我尝试了很多不同的东西,包括一个while循环,并设法让我的浏览器一遍又一遍地崩溃。所以现在我正在寻找你的帮助!

Do I need to use a while loop? I have tried many different things including a while loop and have managed to crash my browser over and over. So now I am looking for your help!

谢谢!!!

推荐答案

标识符主要用于挑选元素,如果你想要一个广泛的组。
我建议你将id转换为类,例如

Identifiers are mainly for singling out elements, if you want a wide group. I suggest you convert the id into a class such as

<a class="test"></a>

然后你可以使用选择器访问所有元素

Then you can access all elements by using the selector

$(".test") // gathers all elements that contain that class

如果你想枚举所有元素,你可以在回调中使用每个函数和两个参数

also if you want to enumerate all the elements you can use the each function with two parameters in the callback

$.each($(".test"), function(a,b){
//a is the index
//b is the element
b.attr("id", a); // to set each element id to their index
});

这篇关于jQuery每个循环重命名一个ID的每个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆