【问题标题】:Jquery mobile listview formated text not working through phpJquery mobile listview格式化文本无法通过php工作
【发布时间】:2014-02-26 20:44:36
【问题描述】:

我在 Javascript 中有以下功能。

function showInfo(str)
{
if (str=="")
{
    document.getElementById("txtHint").innerHTML="";
    return;
} 

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("txtHint").innerHTML=xmlhttp.responseText;
    }
}

xmlhttp.open("GET","information.php?q="+str,true);
xmlhttp.send();
}

我创建了一个选择,这样一旦选择了某个内容,它就会显示一个列表。

<ul>
        <li><select name="days" id="days" onchange="showInfo(this.value)" data-native-menu="false">
            <option value="">  select   </option>
            <option value="1"> option 1 </option>
            <option value="2"> option 2 </option>
        </select></li>
</ul>

information.php 获取值并查询数据库以在列表中获取我想要的信息。这一切都很好,但是当我想显示列表时,它的格式不正确。

echo '
<ul data-role="listview">
<li>
    <h3>Author:'.$the_author.'</h3>
    <p><b>Description:</b>'.$description.'</p>
    <p class="ui-li-aside">Last update:'.$date.'</p>
</li>
    //Etc.
</ul>
';

现在显示的列表应该与 jquery mobile demos (http://demos.jquerymobile.com/1.0.1/docs/lists/lists-formatting.html) 中的列表相似

但是它看起来像一个标准的 html 列表。

【问题讨论】:

    标签: javascript php html jquery-mobile


    【解决方案1】:

    因为您正在动态创建 &lt;UL&gt; 元素而不仅仅是列表项,所以您需要在设置 HTML 后在 txtHint 上调用 enhanceWithin() (http://api.jquerymobile.com/enhanceWithin/)

    而不是这个:

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
    }
    

    试试

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            $('#txtHint').append(xmlhttp.responseText).enhanceWithin();
        }
    }
    

    这是一个带有模拟 AJAX 调用的工作 DEMO

    【讨论】:

    • 应该在哪里准确添加。
    • 我不知道它是在 javascript 中,还是在我的 php 代码中。我现在正在处理这个问题。抱歉,这是一个非常基本的问题
    • 这是一个 JavaScript 语句,必须在添加列表视图项后运行。如果我理解你的代码......在 document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 行之后在 onreadystatechange 中(这不是您要添加项目的地方吗?)
    • 是的,但是它没有格式化列表。我在 php 文件中的列表中添加了一个 id (echo "
        ";) 并添加了行 $(#infolist" ).listview("refresh"); 在javascript中
    • $("#infolist").listview("refresh");应该是正确的(我假设 # 之前缺少引号是错字)。你可以尝试添加一个 setTimeout 来延迟它,看看它是否是一个时间问题......我真的不知道你的项目是如何设置的,所以很难说问题是什么。
    猜你喜欢
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    相关资源
    最近更新 更多