douzhicai7148 2015-06-28 12:36
浏览 51
已采纳

jQuery:<ul>中的动态插入将无法正常工作

I'm writing a page in jQuery Mobile and I have an Unordered List which contains elements like these:

<ul id="ul1" data-role="listview" data-theme="d">
            <li>
                <a>
                <h1>Who wants to live forever</h1>
                    <p>Queen</p>
                </a>
            </li>
            <li>
                <a>
                <h1>Personal Jesus</h1>
                    <p>Depeche Mode</p>
                </a>
            </li>
        </ul>

But I want to insert elements DYNAMICALLY, searching in a MySQL database. It works, but the elements that are inserted in the UL DON'T look like the default LI in jQuery Mobile: they appear as simple text thrown in the UL. The jQuery Mobile's "Graphic" is not represented. Here's the codes:

Ajax function for searching through php:

<script>
function ricerca() {
    str = document.getElementById("search").value;
    if (str == "") {
        document.getElementById("ul1").innerHTML = "Nulla";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("ul1").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","cerca.php?ricerca="+str+"&tipo="+document.getElementById("select-choice-0").value,true);
        xmlhttp.send();

    }

Here's the lines of code (in the PHP) that writes the contents in the UL:

// output data of each row
    while($row = $result->fetch_assoc()) {
        if($inizio == 0)
        {if ($row['CodISWC'] != $prev_iswc) {
           echo "<li>
                <a>
                <h1>". $row["NomeCanzone"]. "</h1>
                    <p>". $row["NomeArtista"];
           $prev_iswc = $row['CodISWC']; 
        }
        else{
            echo ", ". $row["NomeArtista"];   
        }

         $inizio = 1;
        }
        else{
            if ($row['CodISWC'] != $prev_iswc) {
           echo "</p>
                </a>
            </li><li>
                <a>
                <h1>". $row["NomeCanzone"]. "</h1>
                    <p>". $row["NomeArtista"];
           $prev_iswc = $row['CodISWC']; 
        }
        else{
            echo ", ". $row["NomeArtista"];   
        }


        }

    }
  • 写回答

1条回答 默认 最新

  • dptn69182 2015-06-28 13:22
    关注

    After updating your list, you need to refresh it, like this:

    $('#ul1').listview('refresh');

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘