在以下示例中单击后,如何禁用可选元素?我还需要改变它的CSS风格。
$(function(){
$("#selectable").selectable({
stop: function() {
var result = $( "#select-result" ).empty();
$( ".ui-selected", this ).each(function() {
var index = $( "#selectable li" ).index( this );
result.append( " #" + ( index + 1 ) );
});
}
});
});

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Selectable - Serialize</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
#feedback { font-size: 1.4em; }
#selectable .ui-selecting { background: #FECA40; }
#selectable .ui-selected { background: #F39814; color: white; }
#selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
#selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<p id="feedback"><span id="select-result">none</span>.</p>
<ol id="selectable">
<li class="ui-widget-content">Item 1</li>
<li class="ui-widget-content">Item 2</li>
</ol>
</body>
</html>
&#13;
我试着这样做
$("#selectable li").index(this).css('text-decoration', 'line-through');
但它确实无效。
这个想法是在元素已经被cliked之后禁用它,这样用户就无法再次选择它。
答案 0 :(得分:3)
有点棘手,但可以做到。
通过将CSS类(pointer-events
)应用于stop
事件处理程序中的选定项目来禁用.selectable-disabled
。
然后使用filter
选项仅允许选择li:not(.selectable-disabled)
个项目。
您可以通过删除.selectable-disabled
类(在下面的演示中单击按钮)将项目恢复到初始状态
$( function() {
$( "#selectable" ).selectable({
filter : 'li:not(.selectable-disabled)',
stop: function(){
var result = '';
$( ".ui-selected", this ).each(function() {
result += " #" + ( $(this).index() + 1 );
}).addClass('selectable-disabled').removeClass('ui-selected');
$( "#select-result" ).html(result || 'none');
}
});
$('#restore').click(function(){
$('.selectable-disabled').removeClass('selectable-disabled');
});
});
&#13;
.selectable-disabled{
text-decoration : line-through;
pointer-events : none;
}
&#13;
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Selectable - Serialize</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
#feedback { font-size: 1.4em; }
#selectable .ui-selecting { background: #FECA40; }
#selectable .ui-selected { background: #F39814; color: white; }
#selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
#selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<p id="feedback"><span id="select-result">none</span>.</p>
<ol id="selectable">
<li class="ui-widget-content">Item 1</li>
<li class="ui-widget-content">Item 2</li>
<li class="ui-widget-content">Item 3</li>
<li class="ui-widget-content">Item 4</li>
<li class="ui-widget-content">Item 5</li>
<li class="ui-widget-content">Item 6</li>
</ol>
<p><button id=restore>Restore</button></p>
</body>
</html>
&#13;
答案 1 :(得分:1)
您只需拨打disable method:
即可停用可选小部件$(this).selectable("disable");
$(function() {
$("#selectable").selectable({
stop: function() {
var result = $("#select-result").empty();
$(".ui-selected", this).each(function() {
$(this).css('text-decoration', 'line-through'); // This line adds strike through formatting.
var index = $("#selectable li").index(this);
result.append(" #" + (index + 1));
});
$(this).selectable("disable"); // http://api.jqueryui.com/selectable/#method-disable
}
});
});
&#13;
#feedback {
font-size: 1.4em;
}
#selectable .ui-selecting {
background: #FECA40;
}
#selectable .ui-selected {
background: #F39814;
color: white;
}
#selectable {
list-style-type: none;
margin: 0;
padding: 0;
width: 60%;
}
#selectable li {
margin: 3px;
padding: 0.4em;
font-size: 1.4em;
height: 18px;
}
&#13;
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Selectable - Serialize</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<p id="feedback"><span id="select-result">none</span>.</p>
<ol id="selectable">
<li class="ui-widget-content">Item 1</li>
<li class="ui-widget-content">Item 2</li>
</ol>
</body>
</html>
&#13;