25

選べるアイテムを取り揃えています。それらの中からプリセットの選択をアクティブにするボタンをどこかに追加したいと思います。それを行う方法はありますか?

私が望むのは、「これらの人を選択する」ように指示してから、すべてのイベントとすべてを通常どおりに起動することです。そのため、これらの選択イベントのすべてを手動で呼び出す必要はありません。

詳細:私が話しているイベントは、APIデモ ページにリストされているものです。

  • 選択済み
  • 選択する
  • 始める
  • 止まる
  • 未選択
  • 選択解除

また、ものを選択する際にセット/クリアされるデータもあると思います。したがって、これらの css クラスを追加するだけではありません。

4

6 に答える 6

28

これは、複数の要素を操作する Alex R のコードのバリエーションです。

http://jsfiddle.net/XYJEN/1/

function SelectSelectableElements (selectableContainer, elementsToSelect)
{
    // add unselecting class to all elements in the styleboard canvas except the ones to select
    $(".ui-selected", selectableContainer).not(elementsToSelect).removeClass("ui-selected").addClass("ui-unselecting");

    // add ui-selecting class to the elements to select
    $(elementsToSelect).not(".ui-selected").addClass("ui-selecting");

    // trigger the mouse stop event (this will select all .ui-selecting elements, and deselect all .ui-unselecting elements)
    selectableContainer.data("selectable")._mouseStop(null);
}

アップデート:

kmk からのコメントによる jQueryUI 1.10: http://jsfiddle.net/XYJEN/163/

于 2012-02-23T20:56:53.730 に答える
14

jQuery UI Web サイト ( http://jqueryui.com/demos/selectable/ ) で選択可能なサンプルを想定すると、次のようになります。

<style>
    #feedback { font-size: 1.4em; }
    #selectable .ui-selecting { background: #FECA40; }
    #selectable .ui-selected { background: #F39814; color: white; }
    #selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
    #selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }
    </style>
    <script>
    $(function() {
        $( "#selectable" ).selectable();
    });
    </script>



<div class="demo">

<ol id="selectable">
    <li class="ui-widget-content">Item 1</li>
    <li class="ui-widget-content">Item 2</li>
    <li class="ui-widget-content">Item 3</li>
    <li class="ui-widget-content">Item 4</li>
    <li class="ui-widget-content">Item 5</li>
    <li class="ui-widget-content">Item 6</li>
    <li class="ui-widget-content">Item 7</li>
</ol>

</div><!-- End demo -->

次のような関数を使用できます。

    function selectSelectableElement (selectableContainer, elementToSelect)
    {
        // add unselecting class to all elements in the styleboard canvas except current one
        jQuery("li", selectableContainer).each(function() {
        if (this != elementToSelect[0])
            jQuery(this).removeClass("ui-selected").addClass("ui-unselecting");
        });

        // add ui-selecting class to the element to select
        elementToSelect.addClass("ui-selecting");

        selectableContainer.selectable('refresh');
        // trigger the mouse stop event (this will select all .ui-selecting elements, and deselect all .ui-unselecting elements)
        selectableContainer.data("selectable")._mouseStop(null);
    }

次のように使用します。

// select the fourth item
selectSelectableElement (jQuery("#selectable"), jQuery("#selectable").children(":eq(3)"));

これは要素のコレクションを選択できるように改善することができますが、これは作業を開始するための出発点です。

于 2011-02-01T14:41:23.950 に答える
4

そこに行きます:

,calc: function() { this._mouseStop(); },
custom: function(guys) {
  var self = this;
  self.selectees.removeClass("ui-selected");
  guys.each(function(){
    $(this).addClass("ui-selecting");
    self._trigger("selecting", {}, {
       selecting: this
    });
  });
  this.calc(); // do the selection + trigger selected
} 

_mouseStopこれをinの後に追加するselectable.jsと、次のように言えます

$("#selectable").selectable("custom", $("#selectable :first-child"));

...またはあなたが好きなもの。

楽しんでください!:)

于 2010-07-13T16:50:58.447 に答える
2

編集:誤解して申し訳ありません。回答を編集しています。

したがって、はい、オブジェクトの選択が ui-selected クラスに対応する可能性があるため、できることは次のとおりです。

$(#button).click(function(){
  $("#element1").addClass("ui-selected");

  .......

});
于 2010-06-29T11:27:48.287 に答える
0

Ionut コードを使用すると、次のようになります。

 $("#selectable li:first").trigger('start').trigger('selecting').trigger('selected'); 

?

于 2010-07-09T16:09:38.707 に答える
0

.trigger('selected')selectedを使用してイベントを手動でトリガーすることはできませんか?

于 2010-07-08T14:10:49.467 に答える