1
$.fn.fillSelect = function (data) {
    return this.clearSelect().each(function () {
        if (this.tagName == 'SELECT') {
            var dropdownList = this;
            $.each(data, function (index, optionData) {
                var option = new Option(optionData.Text, optionData.Value);
                if ($.browser.msie) {
                    dropdownList.add(option);
                } else {
                    dropdownList.add(option, null);
                }
            });
            // code for access "selectedindex" 
        }
    });
};

上記は、jQuery を使用してドロップダウンを動的に生成するためのコード スニペットです。

保存された値を以前に表示するために、selectedIndexプロパティ値を動的に設定する必要があります。// code for access "selectedindex"そのコードを上記のコード内に挿入します。では、 selectedIndexプロパティをどのように設定できdropdownListますか?

4

2 に答える 2

2

selectedIndexプロパティを他のプロパティと同じように設定できるはずです。

dropdownListそれがHTMLSelectElementであると仮定すると、が機能するdropdownList.selectedIndex = 5;はずです。

于 2013-03-12T11:29:39.210 に答える
1

私はコードのこのビットでこれを行います:

$.each(data, function (index, optionData) {
    var option = new Option(optionData.Text, optionData.Value);
    if (/* code to determine if this is the chosen one */) {
        option.setAttribute("selected", "selected");
    }
    if ($.browser.msie) { /* etc */

関連するオプションで選択した属性を設定しています。

于 2013-03-12T11:30:47.580 に答える