Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery向类添加计数_Jquery_Count - Fatal编程技术网

jQuery向类添加计数

jQuery向类添加计数,jquery,count,Jquery,Count,我已经沉着,尝试和调整这个代码超过2小时了,但我仍然没有接近弄明白它 我试着做以下几件事 如果div有class.item1,.item2,.item3为每个类添加css样式,但我需要.item(number)自动获取计数,因为在我的脚本中,我有使用.item(number)类自动创建的div 我只需要“.single attendee wrapper”“have”“.item+(I+1)”,但我无法让它工作 最新修订- /* Inline validation */ .on( 'blur ch

我已经沉着,尝试和调整这个代码超过2小时了,但我仍然没有接近弄明白它

我试着做以下几件事

如果div有class.item1.item2.item3为每个类添加css样式,但我需要.item(number)自动获取计数,因为在我的脚本中,我有使用.item(number)类自动创建的div

我只需要“.single attendee wrapper”“have”“.item+(I+1)”,但我无法让它工作

最新修订-

/* Inline validation */
.on( 'blur change', '.input-text', function() {
    $(".single-attendee-wrapper").each(function(i){
        var $this = $(this);
        var validated = true;

        if ( $(".cart > .item" + (i+1)) ) {
            if ( $this.val() == '' ) {
                $(".item" + (i+1) + " .count").css('background','#ED616A');
                $(".item" + (i+1) + " .count").css('color','#fff');
                validated = false;
            }
        }
        if ( validated ) {
                $(".item" + (i+1) + " .count").css('background','#D1D3D4');
                $(".item" + (i+1) + " .count").css('color','#808285');
        }

    });
} )
这是我到目前为止的片段

.on( 'blur change', '.input-text', function() {
    var count = 0;


    $(".single-attendee-wrapper .input-text").each(function(){
        var $this = $(this);
        var validated = true;
        count++;

        if ( $(this) ) {
            if ( $this.val() == '' ) {
                $('.item'+ count + ' .count').css('background','#ED616A');
                $('.item'+ count + ' .count').css('color','#fff');
                validated = false;
            }
        }
        if ( validated ) {
                $('.item'+ count + ' .count').css('background','#D1D3D4');
                $('.item'+ count + ' .count').css('color','#808285');
        }

    });
} )
在我的剧本中,这部作品非常完美

$(".single-attendee-wrapper").each(function(i) {
        $(this).addClass("item" + (i+1));
    });
HTML


1.
2.
3.

如果要选择所有以
项{*}
开头的类,可以使用“”:

演示


你能添加一些HTML供我使用吗?我无法准确理解您的问题所在。我已成功将“.item(number)”添加到“.single attendee wrapper”,现在我想让脚本找到所有“.item(number)”并将css添加到其中。我可以通过单独输入所有的.item类来完成这项工作,但为了节省时间,我想找到一个awser来修复。这很好,但如果没有任何HTML来分析代码,我仍然无法真正帮助您。如果我针对所有的项,但我想使用相同的项(编号)向div添加样式,这将是可行的。嗯,那我还是不明白你的意思。你能重新表述一下你的问题吗?我有12个div,每个div都有自己的类,例如item1、.item6、.item12我想使用类号将css样式添加到单个div中,例如div has.item8需要添加样式。item8,我希望这有助于你理解我的问题。那么你的问题实际上是
$('.item'+count+'.count')
失败?是的,看起来计数的数字不正确
<div class="single-attendee-wrapper item1">
<div class=" product-addon product-addon-one-attendee" style="display: block;">
<div class="count" style="">1</div>
</div>
<div class="single-attendee-wrapper item2">
<div class=" product-addon product-addon-two-attendees" style="display: block;">
<div class="count" style="">2</div>
</div>
<div class="single-attendee-wrapper item3">
<div class=" product-addon product-addon-three-attendees" style="display: block;">
<div class="count" style="">3</div>
</div>
$("div[class^='item']")