最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery UI object type for "ui" object passed to the callback function? - Stack Overflow

programmeradmin3浏览0评论

In jQuery UI, what is the type/contents of the "ui" object passed to the callback function of alot of the event methods, and how do I use it?

For example, the "selectable" demo, the event "selected" gets passed two params. "Event" and "UI". I am trying to use it as follows:

    $("#selectable").selectable({ 
      selected: function(event, ui) { 
        $(ui).find("input").attr('checked', true); 
      } 
    });

(here is the html:)

<ol id="selectable"> 
    <li class="ui-state-default"><input type="checkbox" value="1" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="2" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="3" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="4" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="5" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="6" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="7" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="8" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="9" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="10" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="11" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="12" /></li> 
</ol> 

But it isn't working. What am I doing wrong? I'm assuming that the ui param is set to an object representing the "li" that is selected, but when I try and use it it doesn't seem to be the case...

Example (edit example)

In jQuery UI, what is the type/contents of the "ui" object passed to the callback function of alot of the event methods, and how do I use it?

For example, the "selectable" demo, the event "selected" gets passed two params. "Event" and "UI". I am trying to use it as follows:

    $("#selectable").selectable({ 
      selected: function(event, ui) { 
        $(ui).find("input").attr('checked', true); 
      } 
    });

(here is the html:)

<ol id="selectable"> 
    <li class="ui-state-default"><input type="checkbox" value="1" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="2" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="3" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="4" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="5" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="6" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="7" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="8" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="9" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="10" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="11" /></li> 
    <li class="ui-state-default"><input type="checkbox" value="12" /></li> 
</ol> 

But it isn't working. What am I doing wrong? I'm assuming that the ui param is set to an object representing the "li" that is selected, but when I try and use it it doesn't seem to be the case...

Example (edit example)

Share Improve this question edited Jan 8, 2010 at 13:18 cmcculloh asked Jan 8, 2010 at 12:28 cmccullohcmcculloh 48.6k42 gold badges108 silver badges137 bronze badges 2
  • Have you tried if it works with $(event.target).find("input") ? – Daff Commented Jan 8, 2010 at 12:38
  • That selects everything, not just the actual target of the event. Weird... – cmcculloh Commented Jan 8, 2010 at 13:17
Add a ment  | 

1 Answer 1

Reset to default 13

From what I understand, (and this is just from making observations and my incredible powers ::cough, cough:: of inference) ui is a object or collection of the elements that are being acted upon within the user interface. In order to access them, you need to pick the one you want to be using, rather than selecting the object as a whole. Eg. in droppable, it's ui.draggable or ui.droppable. In your example, ui.selected is what would work.

$("#selectable").selectable({
    selected: function(event, ui) {
       $(ui.selected).find("input").attr('checked', true);
    }
});

Hope that answers your question.

发布评论

评论列表(0)

  1. 暂无评论