2010-07-26 84 views
0

我有一個問題,在我的控制器中,我將值設置爲集合並將其存儲在ViewData中。例如:mvc2,查詢和qtip問題

ViewData["ex1"] = new SelectList(ex1); // a simple collection 
ViewData["ex2"] = new SelectList(group.Members, "Id", "Gender"); 

我將這些傳遞給我的視圖,並像這樣循環。例如:

<div id="divListBox" style="overflow:auto; border:1px solid #CCCCCC; padding-left:5px; background-color:white; height: 130px"> 

      <% 
      var list = this.ViewData["e2x"] as SelectList; 
      var pList = this.ViewData["ex1"] as SelectList; 

      foreach (var p in list) 
      { 
       foreach (var pil in pList) 
        { 
        if(pil.Text.Contains(p.Value)) // ex1 does contains ex2 
        { 
         %> 
         <input id="cbPerson" type="checkbox" value="<%= p.Value %>" /> 
         <label for="cbPerson"><%= p.Text %></label> 
         <input id="cbPersonInfo" type="hidden" value="<%= pil.Text %>" /><br /> 
         <% 
        } 
        } 
      } 
      %> 
     </div> ... 

這裏是我的jQuery。例如:

$('#divListBox > input').each(function() { 
     var personInfo = $('#cbPersonInfo').val(); 
     $(this).append($('personInfo')); 
      $('*').qtip('hide'); 
      $('#divListBox label').each(function() { 
       $(this).qtip({ 
        content: personInfo, 
        show: 'mouseover', 
        hide: 'mouseout', 
        style: { 
         classes: 'ui-tooltip-custom', 
         tip: true 
        }, 
        position: { 
         my: 'left bottom', 
         at: 'top right' 
        } 
       }); 
      }); 
     }); 

如果我設置我的輸入類型「隱藏」到「文本」,我看到每一個正確的信息。 Hoever,當我將鼠標懸停在他們身上時,第一條信息顯示爲所有人的工具提示。我認爲這可能是我的jQuery,但我不太確定。我一直在處理這個問題幾個小時,但仍然沒有。任何人都可以幫忙嗎?

回答

2

的foreach(!ID始終是唯一的)

id="cbPerson" type="checkbox" value="<%= p.Value %>" /> 
<label for="cbPerson"><%= p.Text %></label> 
id="cbPersonInfo" type="hidden" value="<%= pil.Text %>" /><br /> 

意味着你有幾次相同的ID,所以當你的ID做一個jQuery選擇素,jQuery的選擇找到

的第一個

如果您將您的人包裝在容器中,則更容易。

我做了一個快速調整你我測試工作

%> 
    <div class="person"> 
    <input id="cbPerson" type="checkbox" value="<%= p.Value %>" /> 
    <label for="cbPerson"><%= p.Text %></label> 
    <input id="cbPersonInfo" type="hidden" value="<%= pil.Text %>" /><br /> 
    </div> 
    <% 

<script type="text/javascript"> 
// for each container with class person 
    $('.person').each(function() { 
    //find the input with class cbPersonInfo which is !!!! IN !!!! $(this): $(this) is now the container 
     var personInfo = $(this).find(".cbPersonInfo").val(); 
     $(this).qtip({ 
      content: personInfo, 
      show: 'mouseover', 
      hide: 'mouseout', 
      style: { 
       classes: 'ui-tooltip-custom', 
       tip: true 
      }, 
      position: { 
       my: 'left bottom', 
       at: 'top right' 
      } 
     }); 
    }); 


</script> 

這段代碼的意思是:與類人每格,發現在div內與類cbPeronInfo和使用它的qtip的值。 (和offcourse鉤qtip的那類)


@Edit

實際上,對於語義的理由,最好使用UL代替(更多邏輯),但我相信你能弄清楚如何改變自己?如果你想否則給我一個標誌:)

+0

感謝Nealv,但是現在的工具提示還很遙遠。有沒有辦法只是懸停在標籤上方,尖端就在它的上方。現在,我認爲,因爲所有內容都放在div內,所以工具提示會顯示div的結尾。我不想將div的寬度調整爲固定寬度,要麼是因爲某些人的職業比其他人長。 – hersh 2010-07-27 17:34:59

+0

你可以把網頁在線嗎?我會修復nealv的css – Nealv 2010-07-27 18:03:14

+0

,我已經知道了。我已經替換了你的...「$(this).qtip({」... with「$(this).find('label')。qtip({」...再次感謝幫助和謝謝你爲我節省了很多時間 – hersh 2010-07-27 18:58:33

0

對於一個你的外C#循環 '的foreach(在plist中VAR PIL){' 將可能重建相同的 '」 />
' 元素數次。

的JavaScript var personInfo = $('#cbPersonInfo').val();選擇(和重新選擇相同的元件反覆,因而有相同的工具提示所有懸停,在這種情況下pil.Text

如果一個以上的元件被分配相同的ID,查詢使用該ID將只選擇在DOM第一個匹配元素。 (http://is.gd/dM4EG

確保頁面中的所有html元素都有唯一的ID。

編輯:解決方案的快速鏈接(http://craigsworks.com/projects/qtip_new/forum/take-tooltip-content-from-the-element-attributes-t-8.html)。今天晚些時候將會更新可能的解

<% foreach (var qt in Model) 
{%> 
    <input id="cbPerson" type="checkbox" value="<%= qt.Value %>" /> 
    <label for="cbPerson" qTip="<%=qt.Text %>"><%= qt.Text %></label> 
    <input id="cbPersonInfo" type="hidden" value="<%= qt.Text %>" /><br /> 
<%} %> 

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('[qTip]').each(function() { // Find all elements that have the qTip attribute (labels) 
      var personInfo = $(this).attr('qTip'); 
      $(this).qtip({ 
       content: personInfo , 
       show: 'mouseover', 
       hide: 'mouseout', 
       position: { target: 'mouse' } 
      }); 
     }); 
    }); 
</script> 
+0

那麼我將如何修復我的代碼讓它工作? – hersh 2010-07-27 12:21:25

+0

@hersh - 你能澄清你想要qtip工作的元素嗎?或者你期望最終的結果是什麼? – Ahmad 2010-07-27 12:51:16

+0

我相信這只是我jquery給我的問題。我嘗試切換我的循環,並再次,相同的結果,這意味着爲每個複選框+標籤顯示正確的信息。我想要qtip做的是顯示每個標籤的描述。這裏是ff中爲我呈現的一個例子。 「列表框輸入 - 複選框」...「標籤 - 當我將鼠標懸停在此上時,應顯示描述」...「輸入 - 隱藏類型,其中有描述」。再次,第一個與第一個複選框/標籤相關的描述顯示了它們的全部。 – hersh 2010-07-27 13:31:44