2010-12-14 60 views
0

当我使用jquery ui自动完成版本1.8.5与jquery mobile alpha 2. 当我从自动完成列表中单击一项时出现错误:

$this.attr("href") is undefined.

有没有人知道它的任何修复?

编辑:

<html> 
    <head> 
     <link rel="stylesheet" type="text/css" href="css/ui-lightness/jquery-ui-1.8.custom.css"> 
     <link rel="stylesheet" type="text/css" href="css/autocomplete.css"> 
    </head> 
    <body> 

     <div id="formWrap"> 
      <form id="messageForm" action="#"> 
       <fieldset> 
        <label id="toLabel">select:</label> 
        <div id="friends" class="ui-helper-clearfix"> 

         <input id="to" type="text"> 
        </div> 

       </fieldset> 
      </form> 
     </div> 
     <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script> 
     <script type="text/javascript" src="js/jquery.mobile-1.0a2.js"></script> 
     <script type="text/javascript" src="js/jquery-ui-1.8.custom.min.js"></script> 


     <script type="text/javascript"> 
      $(function(){ 

       var availableTags = [ 
         "ActionScript", 
         "AppleScript", 
         "Asp", 
         "BASIC", 
         "C", 
         "C++", 
         "Clojure", 
         "COBOL", 
         "ColdFusion", 
         "Erlang", 
         "Fortran", 
         "Groovy", 
         "Haskell", 
         "Java", 
         "JavaScript", 
         "Lisp", 
         "Perl", 
         "PHP", 
         "Python", 
         "Ruby", 
         "Scala", 
         "Scheme" 
        ]; 


       //attach autocomplete 
       $("#to").autocomplete({    

        source:availableTags 
        , 

        //define select handler 
        select: function(e, ui) { 
         var contact = ui.item.value; 
         createSpan(contact); 
         $("#to").val("").css("top", 2); 
         return false; 
        }   
       }); 

       }); 

      function createSpan(contact){ 
       //create formatted friend    
       span = $("<span>").text(contact)    
       //add contact to contact div 
       span.insertBefore("#to"); 
      } 
     </script> 

    </body> 
</html> 
+0

链接到一个页面,显示它的发生将是有益的,因为我为本地工作 – naugtur 2010-12-14 09:34:59

+0

不能发送URL。代码很长。 示例可以是任何包含'jquery.mobile-1.0a2.js'的简单jquery自动完成。 – 2010-12-15 10:16:03

+0

在上面添加了一个示例代码。 – 2010-12-15 11:00:54

回答

1

修改jquery.mobile-1.0a2.js:增加了一个检查,看是否href是未定义 代码

$("a").live("click", function(event) { 
      //(START: My added code) 
      if($(this).attr("href")==undefined){ 
       //for links created for interaction - ignore 
       return false; 
      } 
      //(END: My added code) 
    //Remaining code follows 

这里面的固定的问题。

0

$(this)$this

+0

在jquery.mobile-1.0a2.js中编号为var $ this = $(this)。 – 2010-12-17 12:33:21

1

嘿甜食,谢谢你指着我在正确的方向。

我觉得最好重写jquery ui代码,而不是直接修改jquery移动源代码(为了可维护性)。以下内容会覆盖呈现自动完成列表中每个项目的jquery-ui方法;它向正在创建的锚元素添加一个值为'#'的href属性。这可以防止未定义的HREF错误:

$('#to').data("autocomplete")._renderItem = function(ul, item) { 
return $("<li></li>") 
    .data("item.autocomplete", item) 
    .append($("<a></a>").attr({href: '#'}).html(item.label)) 
    .appendTo(ul); 

}