6

次のコードがあります。

<select>
<option value="Type 1">Type 1</option>
<option value="Type 2">Type 2</option>
<option value="Type 3">Type 3</option>
<option value="Other">Other</option>
</select>

<input type="text" id="other" />

私がしたいのは、jQuery を使用して下のテキスト ボックスをデフォルトで非表示にし、ユーザーがドロップダウンから他のオプションを選択した場合に表示することです。

4

4 に答える 4

11

ここではCSSは必要ありません。

$('#sel').change(function() {
    var selected = $(this).val();
    if(selected == 'Other'){
      $('#other').show();
    }
    else{
      $('#other').hide();
    }
});
于 2010-03-24T13:42:19.987 に答える
7
<select id="sel">
<option value="Type 1">Type 1</option>
<option value="Type 2">Type 2</option>
<option value="Type 3">Type 3</option>
<option value="Other">Other</option>
</select>

<input type="text" id="other" style="display: none;" />

$('#sel').change(function() {
    $('#other').css('display', ($(this).val() == 'Other') ? 'block' : 'none');
});
于 2010-03-24T11:39:15.953 に答える
0

これを試して:

<select id="selectbox_id">
<option value="Type 1">Type 1</option>
<option value="Type 2">Type 2</option>
<option value="Type 3">Type 3</option>
<option value="Other">Other</option>
</select>

<input type="text" id="other" />

JQuery:

$(function(){
  // hide by default
  $('other').css('display', 'none');

  $('selectbox_id').change(function(){
   if ($(this).val() === 'Other') {
     $('other').css('display', 'block');
   }
 });
});
于 2010-03-24T11:40:47.970 に答える