2012-07-15 97 views
2

我有一個JQuery函數可以在添加或刪除href屬性時改變按鈕的顏色。它可以完美運行,如果我用模糊的方式啓動它,就像將jquery ui自動完成選擇傳遞給變量

$("#name").blur(function() { 
     var standards_name = $("#name").val(); 
     var standards_link = $("#standardslink"); 
     var update_log = $("#update_log"); 
     if(jQuery.inArray(standards_name, standards) != -1) { 
      if(companyid !== null) { 
       standards_link.attr("href", basic_href + "?standards_name=" + standards_name + "&company_id=" + companyid); 
      } else { 
       standards_link.attr("href", basic_href + "?standards_name=" + standards_name); 
      } 
      standards_link.css("color","#2EE52B"); 
     } else if(standards_name == "") { 
      standards_link.removeAttr("href"); 
      standards_link.css("color","black"); 
      update_log.hide(); 
     } else { 
      standards_link.removeAttr("href"); 
      standards_link.css("color","#FF0011"); 
      update_log.hide(); 
     } 
    }); 

但希望它可以從自動完成下拉列表中進行選擇。這是我無法進一步得到的地方。如果我手動設置變量「standards_name」,粘貼的代碼可以工作,例如var standards_name = "xxxx";但我想ui.item.ticker_name值傳遞給變量。 (以下簡稱「標準」陣列「companyid」設置與PHPjson_encode和更早版本的腳本傳遞和工作得很好)

下面的代碼:

$(function() { 

     $("#name").autocomplete({ 
      source: "../autocomplete.php", 
      minLength: 2, 
      select: function(event, ui) { 
      $('#name').val(ui.item.ticker_name); 
      $('#mktcap').val(ui.item.mkt_cap); 
      var standards_name = ui.item.ticker_name; 
      var standards_link = $("#standardslink"); 
      var update_log = $("#update_log"); 
      if(jQuery.inArray(standards_name, standards) != -1) { 
       if(companyid !== null) { 
        standards_link.attr("href", basic_href + "?standards_name=" + standards_name + "&company_id=" + companyid); 
       } else { 
        standards_link.attr("href", basic_href + "?standards_name=" + standards_name); 
       } 
       standards_link.css("color","#2EE52B"); 
      } else if(standards_name == "") { 
       standards_link.removeAttr("href"); 
       standards_link.css("color","black"); 
       update_log.hide(); 
      } else { 
       standards_link.removeAttr("href"); 
       standards_link.css("color","#FF0011"); 
       update_log.hide(); 
      } 
      }  

     }); 
}); 

回答

4

最簡單的辦法就是欺騙 在選擇處理程序添加

$("#name").attr("standards_name",ui.item.ticker_name); 

,並在第一個塊,你可以簡單地

var standards_name = $("#name").attr("standards_name"); 

但更好的使用jQuery數據功能保存的所有對象:-)

+0

像撕裂後,我的頭髮我意識到ui.item.ticker_name必須是UI的20小時。 item.value包含自動完成中的任何值。我只是假定它包含一個值,因爲正確的值傳遞給了正確的文本框,但是它會自動傳遞,無論它是否未定義。所以我改變了自動完成源代碼,並簡單地用'$('#name')觸發了模糊事件。trigger('blur');'如果沒有定義它,obvisouly不會工作。既然你沒有其他人能夠解決這個問題,我會給你一個試試的答案;) – NewInTheBusiness 2012-07-18 02:54:29

1

使用

$("#name").autocomplete({ 
source: function (request, response) { 
/*Souse code here*/ 
}, 
select: function (e, i){ 
/*Selection code here inorder to get value use i.item.val*/ 

} 
})