jQuery 将升序数字添加到 ID
我有一个生成 div 的表单。我已经为每个 div 应用了一个基本 ID,但我需要向它们添加数字以区分它们。
我已经阅读了 Index 并查看了其他示例,但我无法让它在这里工作,当我放入 num 变量时,它似乎抛出了一个不稳定的问题。无论如何,我取出了破坏它的代码。
这是小提琴,http://jsfiddle.net/clintongreen/BMX4J/13/
谢谢
I have a form that generates divs. I have applied a base ID to each div but I need to add numbers to them to differentiate between them.
I have read up on Index and looked at other example but I can't get it to work here, it seemed to throw a wobbly when I put the num variable in. Anyway I took out the code that was breaking it.
Here is the fiddle, http://jsfiddle.net/clintongreen/BMX4J/13/
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
val()
这里不正确。val()
用于输入元素。当您这样做时:
将其更改为:
并在函数外部执行
var num = 0;
。val()
is not correct here.val()
is for input elements.When you do:
Change it to:
And do
var num = 0;
outside of the function.听起来您想通过向基本 id 添加某种计数器来使创建的
div
中的每个 id 都唯一。如果是这样,最简单的方法是使用两个函数来执行这两个操作。让计数器位于外部函数中,并让内部函数作为捕获计数器并重用它的点击处理程序。小提琴:http://jsfiddle.net/BMX4J/17/
It sounds like you want to make every id in the created
div
unique by adding a counter of sorts to a base id. If so the easiest way to do this two use two functions. Have the counter be in the outer function and let the inner function be the click handler which captures the counter and reuses it.Fiddle: http://jsfiddle.net/BMX4J/17/
您可以尝试使用容器的子计数将升序数字附加到新元素:
You could try using the child count of the container to append ascending numbers to new elements: