JQuery UI 可选:预选列表项
我正在尝试实现可选择列表中第一项的点击。我可以添加CSS 但当我尝试预先选择第一个项目时,无法让单击事件正常工作(我需要单击,因为 onclick 事件有一个 ajax 调用来填充另一个 div 上的相关信息..) 我什至尝试使用触发方法,但无法在列表中的第一项上使用该方法。代码是标准的东西。
<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>
Jquery 函数
$(function() {
$( "#selectable" ).bind("mousedown", function (e) {
e.metaKey = false;
}).selectable({
selected: function(event, ui) {
alert("selected");
},
stop: function() {
var result = $( "#select-result" ).empty();
$( ".ui-selected", this ).each(function() {
var index = $( "#selectable li" ).index( this );
result.append( " #" + ( index + 1 ) );
});
}
});
});
我可以将 css 应用到第一个列表项,如下所示
$('li:first-child').select().addClass("ui-selected");
,但我无法让单击函数正常工作(单击将调用“选定的”匿名回调。)任何指针都会有帮助/。
I am trying to implement the click of the first item of the selectable list.I can add the CSS
but unable to get the click event working when i try to pre select the first item (I need the click since ,onclick event has an ajax call to populate related information on another div..)
I even tried to use the the trigger method , but couldnt get this working on the fist item on the list. The code is standard stuff.
<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>
Jquery function
$(function() {
$( "#selectable" ).bind("mousedown", function (e) {
e.metaKey = false;
}).selectable({
selected: function(event, ui) {
alert("selected");
},
stop: function() {
var result = $( "#select-result" ).empty();
$( ".ui-selected", this ).each(function() {
var index = $( "#selectable li" ).index( this );
result.append( " #" + ( index + 1 ) );
});
}
});
});
I can apply the css to the first list item like this
$('li:first-child').select().addClass("ui-selected");
But i cant get the click function to work (The click would invoke the 'selected' anonymous call back.) Any pointers would be helpful/.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番尝试,我设法让它工作。发布解决方案,因为它可能有用。
我想模拟鼠标单击可选择列表中的第一项。为此,我们首先需要绑定。
一旦执行此操作,我们需要触发单击。我将其放入创建回调中,一旦可选择,就会调用该回调形成。
Jquery 的伟大之处在于它非常直观。
After some trying i managed to get it working.Posting the solution as it may be useful.
I want to simulate the mouse click on the first item on the selectable list.For that we first need to bind
Once you do that we need to fire the click.I put this in the create callback which is called as soon as the selectable is formed.
the great thing about Jquery is that its so intutive.