2009-12-18 129 views
1

我只想動態投放指標,這是正確的計算,所以我知道我的變量的值是沒有問題的,但它不工作:將變量在jQuery選擇

我變「parentIndex '存儲我想要在下面選擇的範圍的索引。我測試了這個變量,它返回了正確的值。

$( 「DropDownMenu跨度:EQ( 」+ parentIndex +「)」)

這是不是把一個變量插入一個jQuery選擇正確的方式?我發現的所有例子都使用這種格式,我錯過了什麼?

(全功能,上下文:)

 $("span input:radio").click(function() { 
     if (($(this).is(":checked")) == true) { 
      var elem = $(this); 
      var parent = $(this).parent(); 
      var aunts = parent.parent().children(); 
      var parentIndex = aunts.index(parent); 
      var position = elem.position(); 
      var topValue = position.top; 
      topValue = topValue - 9; 
      $(".DropDownMenu").css({ "top": "-" + topValue + "px" }); 
      $(".DropDownMenu span").css("background-image", "none"); 
      parent.css({ "background": "#f3f1e7 url(assets/images/branding/DropDownArrow.gif) no-repeat right" }); 
      $(".DropDownMenu span:eq("+parentIndex+")").css({ "background": "#f3f1e7 url(assets/images/branding/DropDownArrow.gif) no-repeat right" }); 

     } 
    }); 

回答

1

試試這個方法:

$(".DropDownMenu span").eq(parentIndex) 

你可以找到這在docs here更多細節。

但是,你的工作方式應該是這樣的,確保你使用FireBug來測試它,也可以用一個不太複雜的函數來測試它的功能。


這將是冷靜,如果JS有這樣的事情sprintf,你可以這樣做:$(".DropDownMenu span:eq(%parentIndex)"

但這只是一廂情願的想法。

+0

然後我就嘗試相反,由於 – 2009-12-18 19:58:38

+0

它的工作,它的行爲不就是我想要的,但是這是我的錯,我的職責是錯誤的 – 2009-12-18 20:03:03

0

你有沒有調試過,看過什麼類型的parentIndex?

嘗試這樣做:

$('.DropDownMenu span:eq('+ parseInt(parentIndex) +')'); 
+0

是的,我做;它實際上一直在工作,我忘了我需要使用索引來找出第n個孩子。我只是很愚蠢。 : - / – 2009-12-18 21:10:29