2

以下のコード行を最適化する方法はありますか?

        $('#state').children('option:not(:first)').remove();
        $('#city').children('option:not(:first)').remove();
        $('#branch').children('option:not(:first)').remove();
        $('#branchAddress').children('option:not(:first)').remove();

すべてのフィールドをコンマ区切りで追加しようとしましたが、うまくいきません。

上記のコードの最適化にご協力ください。

4

4 に答える 4

1

私は次のようなものを試してみます:

$('#state, #city, #branch, #branchAddress')
    .each(function(i,item){$('option:not(:first)',$(item)).remove();});

またはこのように:

$('#state, #city, #branch, #branchAddress').find('option:not(:first)').remove();

ついに(しかし、私はそれを最適化とは呼びません;-)

$('#state option:not(:first), #city option:not(:first), #branch option:not(:first), #branchAddress option:not(:first)').remove();
于 2012-12-03T14:30:35.103 に答える
0

試す

$('#branchAddress #state #city #branchAddress').children('option:not(:first)').remove();

セレクターに複数の要素を含めることができます。

于 2012-12-03T14:22:39.057 に答える
0

上記のすべてに同じクラスを追加し、ID を複数回参照する代わりに一度参照する

于 2012-12-03T14:23:15.700 に答える
0

次のようにする必要があります(jQuery multiple-selector apiも参照)

$('#state, #city, #branch, #branchAddress').children('option:not(:first)').remove();
于 2012-12-03T14:23:33.857 に答える