为什么ui.item.attr("id")返回未定义的值? [英] why ui.item.attr("id") return undefined value?

查看:121
本文介绍了为什么ui.item.attr("id")返回未定义的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用<ul><li>之间的可排序,但是当我尝试获取ui.item.attr("id")时我却未定义,但是其他变量也可以正常工作,例如$(this).attr('id')'+ui.sender.attr('id'),但是我正在搜索<ul>拖动元素的位置.

I am working with sortable between <li> of <ul> but when I try to get ui.item.attr("id") I got undefined but other variable are working well like $(this).attr('id') and '+ui.sender.attr('id') but I am searching the index of the <ul> where the element dragged from.

我的js文件:

 $( init );
      function init() {

            $(".list-items").sortable({
                 connectWith: '.list-items',
                 items: "li:not(.item.new)",
                 placeholder: 'place-holder',
                 scroll: false,
                 tolerance: "pointer",update: function (event, ui) {
                 //alert($(this));
            }, 
            receive : function(e, ui) { 
                 alert('zone : '+ ui.item.attr("id"));            

                 alert('Where the item is dropped : '+$(this).attr('id')); // Where the item is dropped
                 alert('from where the item is dragged : '+ui.sender.attr('id')); // from where the item is dragged

                 if(ui.item.text()=="51173115") {

                      jConfirm('item '+ui.item.context.id+' capacite Cuisson epuise vous voulez continue comme meme ?', 'alerte', function(r) {     
                  if(r)           
                  {

                  }
                  else
                  {
                       $(ui.sender).sortable('cancel'); // refuse element to be dropped
                  }
           }
      }

 }).disableSelection();

        }

我的jsf代码:

<div class="gauchedragdrop"> 

        <p class="ui-widget-header" style="margin:0;padding:5px;color: #2779bb;">tableof</p>
          <ul id="ulgauche" class="list-items ui-sortable " > 
            <ui:repeat value="#{gestionduplanning.listzp01Equipe}" var="vari" >
                    <li class="item"><a  title="#{vari.ordre.toString()}">#{vari.ordre.toString()}</a></li>
            </ui:repeat>            
          </ul>    
</div> 
<div class="droitedragdrop">

            <p:dataTable var="op" value="#{ gestionduplanning.EQlistoneequipe.get(0).listop}" style="width: 700px;float: left;">

                <p:column headerText ="les of" style="width:550px;"> 
                       <ul id="uldroite" class="list-items ui-sortable">  
                        <ui:repeat value="#{op.orderedOf}" var="vari" >  
                            <li class="item"  ><a  title="#{vari}">#{vari}</a></li>
                        </ui:repeat>                        
                        </ul> 
                </p:column>
               </p:dataTable>

        </div>

为什么我获得未定义的值以及如何获得该值?

why i get undefined value and how can I get this value ?

推荐答案

问题是ui.item是对象的集合,因此您需要使用ui.item.context.id来获取作为拖动元素的项目的ID. ,如 jQuery Sortable文档中所述:

The problem is that ui.item is a collection of objects, so you need to use ui.item.context.id to get the id of the item that is the dragged element, as specified in the jQuery Sortable documentation:

ui.item

ui.item

类型:jQuery

代表当前拖动元素的jQuery对象

The jQuery object representing the current dragged element

因此,您的案例ui.item将是被拖动的<li>,并且由于它们上没有设置id属性,因此ui.item.context.id将始终为空.

So your case ui.item will be the <li> that are dragged and since they do not have an id attribute set on them ui.item.context.id will always be empty.

如果在li上设置ID,则可以访问它: http://jsfiddle.net/qj4GG/2/

If you set an id on li then you can access it: http://jsfiddle.net/qj4GG/2/

您到底想获得什么财产?您需要它做什么?

What property exactly are you trying to get? What do you need it for?

这篇关于为什么ui.item.attr("id")返回未定义的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆