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' );
}
}
);
}
}
}
);
}
);
});