如何禁用可选列表的特定元素

Kom*_*lan 4 jquery-ui jquery-ui-selectable

我想禁用可选列表的特定元素.我可以禁用整个列表,但是当我尝试使用特定元素时,它不起作用.

$('#disableButton').click(function(){  

    $('#selectable li#b ').selectable('disable');
});
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/Komlan/RYWaZ/1/

这是我的代码

    //reset seats
function resetSelect(){
    var options = { filter: "li.selectable" };
$( "#selectable").selectable(options);               

}
//Ajax get taken seats
$('input[name="choice"]').change(function(){
    resetSelect();
    var sc_id = $('input:radio[name=choice]:checked').val();

    $.ajax({
        url: 'seatings.php',
        data:{ sc_id:sc_id},
        type: "POST",
        dataType:'text',
        success: function(data){
             var id = data.split(",");
             for (var i=0;i<id.length -1;i++){
             alert(id[i]);
                var string = "#"+id[i];
                $(string).css("color","red");
                $('#selectable li'+string).removeClass("selectable ui-selected"); 
             }
                var options = { filter: "li.selectable" };
            $( "#selectable" ).selectable('destroy').selectable(options);                
        }

    }); 
});
Run Code Online (Sandbox Code Playgroud)

总而言之,每次我的单选按钮组发生更改时,我都会获得一系列id并逐个禁用它们.

Mar*_*num 6

没有直接的方法可以做到这一点(AFAIK),但这里有一点你可以使用的黑客(并记住,它只是一个黑客,也许不是最好的):

添加css类"selectable"(或任何你想要的):

<ol id="selectable">
  <li class="ui-widget-content selectable" id="ok"> 1</li>
  <li class="ui-widget-content selectable"> 2</li>
  <li class="ui-widget-content selectable"> 3</li>
  <li class="ui-widget-content selectable"> 4</li>
  <li class="ui-widget-content selectable"> 5</li>
  <li class="ui-widget-content selectable"> 6</li>
  <li class="ui-widget-content selectable"> 7</li>
</ol>
Run Code Online (Sandbox Code Playgroud)

然后在该css类上使用过滤器:

// Create a filter to only make <li> with the specified css class, selectable.
var options = { filter: "li.selectable" };
$( "#selectable" ).selectable(options);

$('#lol').click(function(){
    console.log('dsfds');

    // Remove/add (toggle) the class used in the filter on the <li> you want to remove the selectable.
    // (Also remove the ui-selected in case it's selected.)
    $('#selectable li#ok').toggleClass("selectable").removeClass("ui-selected");

    // Now destroy the selectable and re-create it with the filter again.
    // We removed the css class from a <li> used in the filter, so it won't be selectable again.
    $( "#selectable" ).selectable('destroy').selectable(options);
});
Run Code Online (Sandbox Code Playgroud)

更新: http ://jsfiddle.net/RYWaZ/7/

参考文献:


小智 5

实际上,您现在可以实现这一目标。你必须将你想要的类添加到你的项目中,例如“不可选择”,并在你的过滤器选项中使用这个 css 技巧: item:not(.unselectable)

网址:

<ul id="selectable">
 <li class="unselectable"> 1</li>
 <li> 2</li>
 <li> 3</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

查询:

$( "#selectable" ).selectable({
 filter:"li:not(.unselectable)"
});
Run Code Online (Sandbox Code Playgroud)

您将能够选择 #selectable 的所有 li 子项,但具有 .unselectable 类的 li 除外。然后你只需要在必要时切换这个类。