2011-07-27 22 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

相關問題