0

では$(document).ready()、特定のドロップダウンの最初の要素を選択するように設定しています。オプションが手動で選択されたかのように、ドロップダウンで変更機能をトリガーする必要もあります。変更がトリガーされると、showTestsByPanel関数が呼び出され、選択したドロップダウン オプションに関連付けられた適切なデータが表示されます。

私は次のことを行っていますが、変更機能をトリガーするのに役立ちません:

$(document).ready(function () {
    $("#lbpanel option:first-child").attr('selected', 'selected');
    $('#lbpanel').trigger('change'); // <-- this is not triggering the change

    $('#lbpanel').change(function () {
        var $this = $(this);
        var parentId = $("#lbpanel").find("option:selected").val();
        $("#selectedTests tr").each(function () {
            showTestsByPanel(parentId, $(this));
        });
    });
});

代わりに試してみ$('#lbpanel').change()ましたが、うまくいきませんでした。

それが役立つ場合:

  • このすべてがモーダル ダイアログ内にあります。
  • これは実際にはallowmultiplefalse に設定されているリストボックスです。

私は何を間違っていますか?

4

2 に答える 2

1

changeハンドラーの作成前にイベントをトリガーしようとしています。だから効果なし。

そのため、イベントハンドラーchange()を登録した後に呼び出しますchange

$(document).ready(function () {
    $("#lbpanel option:first-child").attr('selected', true);
    $('#lbpanel').change(function () {
        var $this = $(this);
        var parentId = $("#lbpanel").find("option:selected").val();
        $("#selectedTests tr").each(function () {
            showTestsByPanel(parentId, $(this));
        });
    }).change(); //added change here
});
于 2013-10-27T11:26:19.920 に答える
1

トリガー ハンドラーが登録された後に変更イベントをトリガーする必要があります。イベントがトリガーされると、既に登録されているハンドラーのみが呼び出されます。

$(document).ready(function () {
    $("#lbpanel option:first-child").attr('selected', 'selected');

    $('#lbpanel').change(function () {
        var $this = $(this);
        var parentId = $("#lbpanel").find("option:selected").val();
        $("#selectedTests tr").each(function () {
            showTestsByPanel(parentId, $(this));
        });
    }).trigger('change'); // need to trigger the event after the handler is added
});
于 2013-10-27T11:26:27.537 に答える