0

我有一段代码,它通过 SOAP 从 SharePoint 列表中提取所有列表项,然后以常见问题解答的形式将它们写入页面。因为这个列表可能会很长,我想把它们写到页面上,这样只有问题是可见的,当你点击一个问题时,那个问题的特定面板和只有那个面板才可见。

除了一小块让我大吃一惊的代码外,我的代码都可以正常工作-

<link rel="stylesheet" href="/sites/someSite/System%20Resources/FAQ2.css" type="text/css"/>
<script src="/sites/someSite/System%20Resources/jquery-1.8.2.min.js"></script>
<script type="text/javascript"> 
$(document).ready(function() {

$(".question").click(function(){
    $("#a"+$(this).attr("id")).slideToggle("slow");});

    var soapEnv =
        "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
            <soapenv:Body> \
                 <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                    <listName>A Big Fat FAQ</listName> \
                    <viewFields> \
                        <ViewFields> \
                           <FieldRef Name='Question_x0020_Description' /> \
                           <FieldRef Name='Response'/> \
                           <fieldRef Name='ID'/>\
                       </ViewFields> \
                    </viewFields> \
                </GetListItems> \
            </soapenv:Body> \
        </soapenv:Envelope>";

    $.ajax({
        url: "/sites/someSite/_vti_bin/lists.asmx",
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        complete: processResult,
        contentType: "text/xml; charset=\"utf-8\""
    });
});

function processResult(xData, status) {
    $(xData.responseXML).find("z\\:row").each(function() {
    var iDesc=$(this).attr("ows_Question_x0020_Description");
    var iResp=$(this).attr("ows_Response");
            var iID=$(this).attr("ows_ID");

            var liHtml = "<div class='question' id='"+iID+"'>" + iDesc + "</div><div class='answer' id='a"+iID+"'><a href='/sites/someSite/Lists/FAQ/DispForm.aspx? ID='"+iID+"'>"+ iResp +"</a></div>";
            alert(liHtml);
            $("#FAQ").append(liHtml);

    });


}
</script>


<h1>A Big Fat FAQ</h1>
<div id="FAQ"></div>

一切正常,除了屏幕上问题的点击事件没有响应。

在发出警报以向我显示它作为 liHtml 变量输出的内容后,我发现它在 iDesc 变量周围插入了一组额外的标签,因此没有什么可点击的。

<div class='question'id='5'>
<div>Why is the sky blue?</div>
</div>

我认为这可能是问题所在。问题描述和响应列都设置为富文本,这导致了备用标签。我将它们都更改为纯文本,它摆脱了多余的 div,我的问答 div 都有正确的 ID,但它仍然无法正常工作。这可能是另一个微小的语法错误,但我在这里画了一个空白,看看还有什么问题。

4

1 回答 1

0

只需移动代码以在“processResult”中切换响应

function processResult(xData, status) {
    $(xData.responseXML).find("z\\:row").each(function() {
    var iDesc=$(this).attr("ows_Question_x0020_Description");
    var iResp=$(this).attr("ows_Response");
            var iID=$(this).attr("ows_ID");

            var liHtml = "<div class='question' id='"+iID+"'>" + iDesc + "</div><div class='answer' id='a"+iID+"'><a href='/sites/someSite/Lists/FAQ/DispForm.aspx? ID='"+iID+"'>"+ iResp +"</a></div>";
            alert(liHtml);
            $("#FAQ").append(liHtml);

    });

$(".question").click(function(){
    $("#a"+$(this).attr("id")).slideToggle("slow");}); 

}
于 2013-02-26T10:24:08.117 回答