0

SELECTオプションリストのさまざまなオプションを選択するjQueryがあるという厄介な問題があります。SELECTED=SELECTED 属性を設定して、最初の項目以外を強制的にデフォルトに設定したいと考えています。

かなり基本的なようで、OPTION タグの他の特性を設定すると、正常に動作します。例えば:

$(this).addClass ('TEST');

デフォルトのオプションに TEST クラスを追加することで、期待どおりに動作します。次のこともできます。

$(this).attr('annoying', "SELECTED");

これにより、「SELECTED」の値を持つ属性「迷惑」が追加されます。悲しいことに、次のことを行うと:

$(this).attr('SELECTED', "SELECTED");

それは完全に無視されます。Chrome のデバッガーにアクセスしてこれを手動で打ち込むと、動作しますが、JS ファイルからは動作しません。:^(

何か案は?

更新 これまでのところ素晴らしい提案ですが、何も機能していないので、何か他のことが間違っているのではないかと思います。詳細なコンテキストを提供するために...これはWordpressサイトであり、QuickEdit機能を使用して、カスタム属性の適切なOPTIONを分類法に表示します。Wordpress を知らない人にとっては、おそらく重要ではありません (この段階では誰でも知っています)。

完全なコードは次のとおりです。

jQuery(document).ready(
function ($) {
    $('.editinline').on ('click', 
    function () {
        var $data_ref = $(this).closest('tr');
        $('.bespoke-item.quick-edit').each (
            function () {
                var col_name = $(this).attr('name');
                var col_value = $data_ref.find ('.' + col_name).text();
                $(this).val(col_value); // this will work for non-enumerated widgets
                if ( $(this).is( "select" ) ) {
                    selected = false; // used as a flag on whether the SELECTED value has been found
                    options = $(this).find ('.select-option');
                    options.each( 
                        function () {
                            if ( $(this).attr('value') == col_value ) {
                                $(this).addClass ('TEST');
                                selected = true;
                                $(this).attr('SELECTED', "SELECTED");
                                this.selected = 'SELECTED';
                            } 
                        }
                    );
                    if ( !selected ) {
                        // The value of the did not match any of the selections (this likely means it wasn't set at all)
                        // In this case revert to the "static-default" setting
                        options.each( 
                            function () {
                                if ( $(this).attr('static-default') ) {
                                    $(this).attr ('SELECTED' , 'SELECTED');
                                    alert ("Found value in static default " + $(this).val());
                                    selected = true;
                                } else {
                                    $(this).removeAttr ( 'SELECTED' );
                                }
                            }
                        );
                    }
                }

            }
        );
    }
);

});

4

1 に答える 1

3

あなたのための3つのオプション(あなたの質問にあるように、それが問題thisの要素であると仮定しています):option

  1. jQuery の最近のコピーを使用している場合はattrvspropselected . の可能性があります (大文字ではなく小文字にする必要があることにも注意してください)

    $(this).prop('selected', true);
    
  2. DOM を直接使用する方が簡単な場合もあります。

    this.selected = true;
    
  3. または、もちろん、要素で使用valして、select選択したいオプションの値に設定します。

    $("selector for the select element").val($(this).val());
    // or better (as `value` is entirely reliable on option elements):
    $("selector for the select element").val(this.value);
    

3 つすべての実例| ソース

于 2012-12-02T16:16:48.690 に答える