首页 > 如何指定.each()遍历的范围?

如何指定.each()遍历的范围?

each会遍历整个页面下相关的元素,但是有没有让它直接跳过最后一个的元素,
或者有没有方法可以让它不去遍历某个指定的元素?(例如id="clone"里面的.vote-preview,但不能改动id="clone"的子元素结构)

function checkoption(){
    $.each($(".vote-preview"),function(i,item){
        var optionlength = $(this).find("p").length;
        if(optionlength < 2){
            $(".checkoption").val(optionlength);
            console.log($(this).html());
            return false;
        }
    });
}

$(document).on('click', '.finish-questionnaire', function(e){
    checkoption();
    if($(".vote-preview").length < 1){
        alert("啥都没填,你还想走?");
        return false;
    }else if($(".checkoption").val() < 2){
        alert("有道题的选项少于2个!");
        return false;
    }else{
        location.href="{:U('index')}";
    }
});

最后一个元素在类似下面的模版里,每执行一次某个onclick事件就会添加一次这段模版,然后加载里面相关的参数,有没有办法可以让这个模版不会被上面的jq遍历到?

<div id="clone" style="display:none">
    <div class="panel panel-info vote-preview {8}">
        <div class="panel-heading">
            <h5><span class="label label-warning {4}">{1}</span>{2}</h5>
        </div>
        <div class="panel-body {3}">
            <div class="input-group" style="max-width:300px;">
                <span class="input-group-addon">最多能选</span>
                <input type="text" name="" class="form-control {7}" placeholder="默认单选,大于1为多选" value="" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" >
                <span class="input-group-btn">
                    <button type="button" class="btn btn-default set-maxoption" onclick="update_question({5})">提交</button>
                </span>
            </div>
        </div>
        <div class="panel-fotter">
            <button type="button" class="btn btn-primary btn-block del-vote" onclick="delete_question({6})">删除该问题</button>
        </div>
    </div>
</div>

clone 的模板一般是不能出现 id 属性的,因为 id 属性在整个文档中需要唯一,不过你可以利用这一点

// 克隆时去掉 id 属性
cloneButton.on("click", function() {
    var newOne = $("#clone").clone().removeAttr("id");
    addToContainer(newOne);
});

// 遍历时
$(".vote-preview").filter(function() {
    return !$("#clone").is(this);
}).each(....);

each函数中的i代表当前索引值,最后一个索引值等于匹配元素的长度减去1,只要设置if条件 当两者 i==length return true就不会改动最后一个匹配元素


给你想遍历的元素加一个className,然后选择这个特定的className元素进行遍历。
你的描述是N个元素,只需要遍历前N-1的元素,那就给这N-1个元素一个className,比如vote-preview-diff,再进行遍历

$('.vote-preview-diff').each(function(i, item) {
    // do something
})

:not()
http://api.jquery.com/not-sel...

:last
http://api.jquery.com/last-se...

最终:
jQuery('.vote-preview:not(:last)')


each 之前先 filter 一下呗。

$(".vote-preview").filter(function(){
      return $(this).parent().attr("id")!="clone";
}).each(...

如果你只是不想遍历最后一项,直接把数组的最后一项干掉就行了。数组的length数组可以直接操作数组。用变量保存选取的元素数组,操作length,length - 1再each。
另外,就是添加模板的时候做判断,或者是添加变量count计数,或者是根据判断条件添加特殊类,这样都方便click的时候对元素数组做判断。这都是理论O(∩_∩)O~我会按照这方式去试一试,不过没有帮你实践,抱歉。有问题再讨论,也在撸代码中...

【热门文章】
【热门文章】