我想做的是计算当前页面中具有相同类的所有元素,然后将其添加到输入表单的名称中。基本上我允许用户单击<span>
然后通过这样做为更多相同类型的项目添加另一个。但我想不出一种方法来简单地用 jQuery/JavaScript 来计算所有这些。
然后我打算将这个项目命名为类似的东西name="whatever(total+1)"
,如果有人有一个简单的方法来做到这一点,我将非常感激,因为 JavaScript 并不完全是我的母语。
应该是这样的:
// Gets the number of elements with class yourClass
var numItems = $('.yourclass').length
顺便说一句,在链接 jQuery 对象上的许多函数调用之前检查 length 属性通常是有益的,以确保我们实际上有一些工作要做。见下文:
var $items = $('.myclass');
// Ensure we have at least one element in $items before setting up animations
// and other resource intensive tasks.
if($items.length)
{
$items.animate(/* */)
// It might also be appropriate to check that we have 2 or more
// elements returned by the filter-call before animating this subset of
// items.
.filter(':odd')
.animate(/* */)
.end()
.promise()
.then(function () {
$items.addClass('all-done');
});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)