2012-01-09 15 views
0

私はこれでチェックボックスをユーザーがクリックしたときに無効にコンボボックスを持っている:は、選択ボックスを無効にすることはできますが、同様のコードで最初の子を選択することはできません

$(this).parent().next('td').children('.xyz').attr("disabled", true); 

私も表示するように同じコンボボックスをリセットしたいと思いますリストの最初の値は、次のようになります。

$(this).parent().next('td').children('.xyz option:first-child').attr("selected", "selected"); 

残念ながら、これはうまくいかないようですが、何が間違っていますか?

+0

テーブルを使用していますか?セルの子ではなく、適切なセレクタを作成します。 –

+0

チェックボックス+コンボボックスがテーブルとして表示されます。要素には個々のidsがあり、使用することができますが、なぜこのメソッドが機能しないのかを知りたいと思います。 – 0x4B1D

+0

[KISS](http://ja.wikipedia.org/wiki/KISS_principle)表示:CSSを使用してインラインとスタイルを使用します。 –

答えて

1

JavaScriptによる選択は少し異なります。 this discussionから、このプラグインを使用する方が簡単でしょう:

/** 
* Select/deselect any matching checkboxes, radio buttons or option elements. 
*/ 
$.fn.selected = function(select) { 
    if (select == undefined) select = true; 
    return this.each(function() { 
     var t = this.type; 
     if (t == 'checkbox' || t == 'radio') 
      this.checked = select; 
     else if (this.tagName.toLowerCase() == 'option') { 
      var $sel = $(this).parent('select'); 
      if (select && $sel[0] && $sel[0].type == 'select-one') { 
       // deselect all other options 
       $sel.find('option').selected(false); 
      } 
      this.selected = select; 
     } 
    }); 
}; 

$(this).parent().next('td').children('.xyz option:first-child').selected(true); 
+0

minitechは動作していないようです。私はメソッドが正しいと確信していますが、問題は、セレクタに 'オプション:first-child'があり、オブジェクトが選択されていないということです(少なくともコンソールによると)。 – 0x4B1D

+0

@ニキータ: 'option:first-of-type'を試してみてください。 – Ryan

関連する問題