2011-07-27 12 views
4

ドロップダウンリストを無効にすることなくドロップダウンリストオプションの変更を制限する方法を教えてください。
私はこのオプションを変更することはできません。そのドロップダウンリストは読み取り専用ではありません。
私の問題は私のサーバーが無効になっ要素あなたが行うことができ無効にすることなくドロップダウンリストの変更を制限する方法

+0

を変更したいですか?その中の値? – Marthin

+0

質問に力を入れればするほど、回答の質と量の両方が向上します。あなたがしようとしていることは本当に非常に不明です。 –

+1

質問を明確にしてください。あなたが本当に求めていることを混乱させます。説明を助けるためにいくつかのコードを使用するかもしれません。 –

答えて

3

Here's my bid

を試してみてくださいjQueryの

var lastSel = $('#foo').val(); 
$('#foo').change(function(){ 
    var $select = $(this), $status = $('#status'); 

    var $selOpt = $select.find('option:selected'); 
    if (!$selOpt.hasClass('disabled')){ 
     lastSel = $selOpt.index(); 
     $status.text('Selection changed to ' + $selOpt.val() + ' [' + lastSel + ']'); 
    }else{ 
     $selOpt.removeAttr('selected'); 
     $select.find('option:eq('+lastSel+')').attr('selected',true); 
     $status.text('Invalid selection, reverting to ' + lastSel); 
    } 
}); 

HTML

<select id="foo"> 
    <option value="">Please select one</option> 
    <option value="a">Option A</option> 
    <option value="b">Option B</option> 
    <option value="c" class="disabled">Option C</option> 
    <option value="d">Option D</option> 
    <option value="e">Option E</option> 
    <option value="f" class="disabled">Option F</option> 
    <option value="g">Option G</option> 
    <option value="h">Option H</option> 
    <option value="i" class="disabled">Option I</option> 
</select> 
<p id="status"></p> 

プラグインのバージョン

(function($){ 
    $.fn.extend({ 
     restrictedSelect: function(disabledClass){ 
      disabledClass = disabledClass || 'disabled'; 

      return this.each(function(i,e){ 
       var $s = $(e); 

       // store the current selection. This is also used as a fall-back 
       // value when the user selects something invalid. 
       $s.data('currentSelection',$s.find('option:selected').index()); 

       $s.change(function(){ 
        var $cs = $s.find('option:selected'); 
        if ($cs.hasClass(disabledClass)){ 
         $cs.removeAttr('selected'); 
         $s.find('option:eq('+$s.data('currentSelection')+')').attr('selected',true); 
        }else{ 
         $s.data('currentSelection',$cs.index()); 
        } 
       }); 
      }); 
     } 
    }); 
})(jQuery); 

$('select').restrictedSelect('invalid-select-option'); 
+0

本当にかっこいいですが、メニューから削除するのではなく、なぜそれを無効にするのですか?なぜユーザーが選択しないと言うだけのものを選択させるのでしょうか? – AlienWebguy

+0

@AlienWebguy:これもできます。まったく同じですが、 '.disabled {display:none; } ' –

+0

しかし、あなたのjqueryは目的を果たしません:) – AlienWebguy

2
$('#my_select').change(function(event){ 
    $(this).val(this.defaultSelected); 
}); 
+0

どのように私は以前の値を設定することができます.... – Reddy

+0

あなたはそれを任意の値に上書きすることができます。以前の値がどこかの変数に格納されている場合は、単純に '$(this)).val( - here - )'に記述してください。例:http://jsbin.com/ulufer/edit – AlienWebguy

2

を読んでいないされている。

//save the value selected when you load the page 
var default = $('#yourselect option:selected').val(); 
//reset the value when the select change 
$('#yourselect').change(function(event){ 
    $(this).val(default); 
}); 
1

この

<select id="selectId" data-default="1"> 
<option value="0">0</option> 
<option value="1">1</option> 
... 
</select> 

$('#selectId').change(function(){ 
    $(this).val($(this).data("default")); 
}); 
+0

http:// www。 w3schools.com/tags/att_option_selected.asp – AlienWebguy

関連する問題