gpt4 book ai didi

jquery - 如何限制下拉列表更改而不禁用它

转载 作者:行者123 更新时间:2023-12-01 00:01:17 26 4
gpt4 key购买 nike

如何在不禁用下拉列表的情况下限制下拉列表选项更改。
意味着我无法更改选项,并且下拉列表不应该是只读的。
我的问题是我的服务器没有读取禁用的元素

最佳答案

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

关于jquery - 如何限制下拉列表更改而不禁用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6846166/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com