3

このフィドルがあり、選択されたボックスの数をカウントするようにしたいと思います。これで、ボックスの数が表示されます。

それを行う方法はありますか??

 $(function() {
    $(".selectable").selectable({
        filter: "td.cs",

        stop: function() {
            var result = $("#select-result").empty();
            var result2 = $("#result2");
            $('.ui-selecting:gt(31)').removeClass("ui-selecting");

            alert($(".ui-selected").length);
            if ($(".ui-selected").length > 4) {
                $(".ui-selected", this).each(function(i, e) {
                    if (i > 3) {
                        $(this).removeClass("ui-selected");
                    }
                });
                return;
            }

            $(".ui-selected", this).each(function() {
                var cabbage = this.id + ', ';
                result.append(cabbage);
            });

            var newInputResult = $('#select-result').text();
            newInputResult = newInputResult.substring(0, newInputResult.length - 1);
            result2.val(newInputResult);
        }
    });
});​

私の問題は、許可された最大選択ボックスの結果を表示することです。たとえば、8 つのボックスを選択して 4 つだけを表示し、最大許容値である 4 の結果を取得した場合。最後に、アラートではなく、div タグなどで表示する

http://jsfiddle.net/dw6Hf/46/

ありがとう

4

1 に答える 1

2

このライブデモのようなメッセージを表示できます

$('#divmsg').html($(".ui-selected").length + " box selected")
if ($(".ui-selected").length > 4) {
         $('#divmsg').html($('#divmsg').html() + ", Selection of only 4 allowed");
         $(".ui-selected").each(function(i, e) {                  
         if (i > 3) {
                        $(this).removeClass("ui-selected");
         }
        });
        return;
  }
于 2012-06-12T09:18:19.103 に答える