【问题标题】:Grabbing values from a different tables and appending to jquery从不同的表中获取值并附加到 jquery
【发布时间】:2012-12-29 13:01:57
【问题描述】:

我正在使用两张表,一张用于产品名称,一张用于产品图像路径... 我能够获取图像路径并将图像动态添加到 html 中。它工作正常。

问题是当我想在悬停时添加图像的标题时,它是顶部的 div 类 图片,我使用jQuery的每个函数更改悬停div上的标题,标题被连接起来 而不是添加到特定图像中。

这是我从表路径附加图像的代码..

<script type="text/javascript">
    $(document).ready(function () {
        $('#mainContentPlaceholder_productPathGridView').hide(); //Hides ImagePath Gridview

        var arr = []; //Creates an Array Path Arr
        $("#mainContentPlaceholder_productPathGridView td").each(function () {  //Loops through GridView Td (Table Columns)
            arr.push($(this).text());
        });

        // Starts adding Images to the  Gallery 
        $.each(arr, function (index, imageLocation) {
            $(".megafolio-container").append('<div class="mega-entry jeans cat-all" data-src="' + imageLocation + '"' + 'data-width="395" data-height="507"><div class="mega-hover"><div class="mega-hovertitle"><h2 class="productNameTitle"></h2><a class="shopButton" href="itemDetails.aspx">Details</a></div></div></div>'); //Apended New  Values
        });

    });
</script>

这是我添加的用于附加 ID 的代码:

<!-- Getting Product Names-->

<script type="text/javascript">
    $(document).ready(function () {
        $('#mainContentPlaceholder_productNameGridView').hide(); //Hides ImagePath Gridview

        var arr2 = []; //Creates an Array Path Arr
        $("#mainContentPlaceholder_productNameGridView td").each(function () {  //Loops through GridView Td (Table Columns)
            arr2.push($(this).text());
        });

        // Starts adding Images to the  Gallery 
        $.each(arr2, function (index, productname) {
         //   alert(productname);
           $(".productNameTitle").text(productname); //Apended New Name
        });
        });
</script>

现在问题存在,因为每个 div 上都连接了 id..

【问题讨论】:

    标签: jquery html append


    【解决方案1】:

    如果我没记错的话..为什么不使用一个循环而不是两个..您正在创建 teo 不同的数组 arr、arr2 并循环遍历它...以显示相同的文本..

    试试这个

    $("#mainContentPlaceholder_productNameGridView td").each(function () {  //Loops through GridView Td (Table Columns)
            $(".megafolio-container").append('<div class="mega-entry jeans cat-all" data-src="' + $(this).text() + '"' + 'data-width="395" data-height="507"><div class="mega-hover"><div class="mega-hovertitle"><h2 class="productNameTitle"></h2><a class="shopButton" href="itemDetails.aspx">Details</a></div></div></div>'); //Apended New  Values
            $(".productNameTitle").text($(this).text());
        });
    

    【讨论】:

    • 它不工作。我想要一个表的图像路径和另一个表的名称路径,附加到 html 的图像路径工作正常,但在名称路径上,名称是连接的,我使用的代码与用于附加图像路径。在名为 productNameTitle 的 div 中,应附加名称。它的附加,但所有的名字都是串联的..请帮助。谢谢
    • 好的,我尝试使用 .html() 方法进行更改,但每个 div 中只显示我设置为 11 的最后一个项目名称...请帮助。
    • 试试这个...$(".productNameTitle").text('

      '+productname+'

      ');.. 得看你的HTML代码来帮你。 .
    • 不,每个 div 中仍然显示 11,是因为我使用的是数字而不是名称吗?我尝试提醒检查值,它们运行良好。但我不知道为什么它不会以正确的格式更改.. 可能是因为主容器 div 也是动态生成的?你能帮我吗,这对我的项目非常重要。谢谢
    猜你喜欢
    • 1970-01-01
    • 2020-02-16
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 2015-12-23
    • 2023-01-25
    相关资源
    最近更新 更多