0

我在控制器中遇到问题,我在集合中设置值并将它们存储在 ViewData 中。例如:

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'
                    }
                });
            });
        });

如果我将“隐藏”的输入类型设置为“文本”,我会看到每个输入类型的正确信息。但是,当我将鼠标悬停在它们上时,第一个信息显示为所有它们的工具提示。我认为这可能是我的 jquery,但我不太确定。我已经处理这个问题几个小时了,但仍然一无所获。有人可以帮忙吗?

4

2 回答 2

2

前锋:

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

意味着您有好几次相同的 ID(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>

这段代码的意思是:对于每个带有 person 类的 div,找到里面带有类 cbPeronInfo 的 div 并将它的值用于 qtip。(并且将 qtip 与该课程挂钩)


@编辑

实际上,出于语义原因,最好使用 UL 而不是(更多逻辑),但我假设您可以自己弄清楚如何更改它?如果你想否则给我一个标志:)

于 2010-07-27T15:43:25.397 回答
0

一方面,您的外部 c# 循环 'foreach (var pil in pList){' 将
可能多次重新创建相同的 '" /> ' 元素。

javascriptvar 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>
于 2010-07-27T11:58:37.963 回答