简体   繁体   English

禁用jquery选择的下拉列表

[英]disable jquery-chosen dropdown

I have a select div that I'm using the chosen jquery plugin to style and add features to (most notably, search). 我有一个选择div,我正在使用所选的jquery插件来设置样式并添加功能(最值得注意的是,搜索)。 The div looks something like this, div看起来像这样,

 <select data-placeholder="add a foobar" id="foobar" style="width: 350px;">
 <option value=""></option>
 </select>

And I'm using the chosen plugin like this, 我正在使用这样选择的插件,

 $('#foobar').chosen();

While some AJAX is loading, I'd like to disable the entire <select> div. 在加载某些AJAX时,我想禁用整个<select> div。 Maybe with something like this, 也许有这样的事情,

 $('#foobar').disable()

or this 或这个

 $('#foobar').prop('disabled', true)

I think you get the idea. 我想你应该已经明白了。

Any ideas on how to do this? 关于如何做到这一点的任何想法? I've tried a number of different things, like using jquery idioms for disabling things, disabling the <select> which just disables the underlying select, not the chosen stuff on top of it. 我已经尝试了很多不同的东西,比如使用jquery惯用法来禁用东西,禁用<select> ,它只是禁用底层选择,而不是在它上面选择的东西。 I've even resorted to manually adding another div with a high z-index to just grey out the box, but I think that this is likely to be ugly and buggy. 我甚至使用手动添加另一个具有高z-index div来使盒子变灰,但我认为这可能是丑陋和错误的。

Thanks for the help! 谢谢您的帮助!

You are disabling just your select , but chosen renders it as divs, and spans, etc. So after disabling your select you need to update the plugin to make the select widget disabled too. 您只是禁用了您的select ,但是选择将其渲染为div和span等。因此,在禁用您的选择后,您需要更新插件以禁用选择窗口小部件。 You can try this way: 你可以这样试试:

$('#foobar').prop('disabled', true).trigger("liszt:updated");

//For non-older versions of chosen you would want to do:

$('#foobar').prop('disabled', true).trigger("chosen:updated");

I found the information here 我在这里找到了这些信息

Fiddle 小提琴

Once you update the widget all it does is it unbinds the click or other events on the plugin and changes its opacity to 0.5. 一旦你更新小部件,它所做的就是取消绑定插件上的点击或其他事件,并将其不透明度更改为0.5。 As there is no real disabled state for a div. 因为div没有真正的禁用状态。

In the lastest version of chosen, liszt:updated is not working anymore. 在所选的最新版本中, liszt:updated不再适用。 You need to use chosen:updated : 你需要使用chosen:updated

$(".chosen-select").attr('disabled', true).trigger("chosen:updated")

Here's a JSFiddle . 这是一个JSFiddle

PSL was correct, but chosen has been updated since. PSL是正确的,但选择后更新。

Put this after you do the disabling: 在执行禁用后将其放入:

$("#your-select").trigger("chosen:updated");
$('#foobar').prop('disabled', true).trigger("chosen:updated");

This works Perfect!!!! 这完美!!!! @chosen v1.3.0 @chosen v1.3.0

$("chosen_one").chosen({
  max_selected_options: -1
});
$(document).ready(function () {
    $("#foobar").chosen().on('chosen:showing_dropdown',function() {
            $('.chosen-select').attr('disabled', true).trigger('chosen:updated');
            $('.chosen-select').attr('disabled', false).trigger('chosen:updated');
            $('.search-choice-close').hide();
    });
    $('.search-choice-close').hide();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM