2016-02-28 29 views
0

我想輸出只有文章如果authorsId = authorId函數返回一個列表 - 問題與過濾輸出

除此之外,整個函數的工作正是我想要的,那就是:

的總體思路是,以限制只能訪問自己的文章。

所以,我的問題是:如何限制結果僅顯示由我們所在頁面的所有者編寫的文章(authorsId = authorId)。

function ArticlesListReturn(returned) { 
    xml = returned.documentElement; 
    var rel = document.getElementById('related_category_articles'); 
    rel.options.length = 0; 
    var status = getXMLData('status'); 
    var title = ''; 
    var id = ''; 
    var authorid = ''; 


    if (status == 0) { 
     alert("%%LNG_jsArticleListError%%" + errormsg); 
    } else { 
     var authorid = document.getElementById("authorid").value; // Serge 

     // authorsid = getNextXMLData('authors',x); 
     for (var x = 0; x < xml.getElementsByTagName('titles').length; x++) { 


     title = getNextXMLData('titles', x); 
     id = getNextXMLData('ids', x); 
     authorsid = getNextXMLData('authors', x); 

     alert(authorsid) // authors of each article - it returns the proper values 
     alert(authorid) // author of the page we are on - it returns the proper value 

     var count = 0; 

     rel.options[x] = new Option(title, id, authorid); // lign that returns results 

     title = ''; 
     id = ''; 
     authorid = ''; 

     } 
} 
+0

您仍然有一個鬆散的'rel.options' – mplungjan

+0

@mplungjan我更新了。至於你關於getNextXMLData的問題,它與其他事情有關(一旦列表產生),我認爲這與問題無關。 rel.options(不確定要理解)。 – Sergelie

+0

'rel.options <<< needs to go - next line is var count = 0;' – mplungjan

回答

0

我懷疑問題是,當你嘗試執行一個條件語句(如果/那麼/人),你是一個數量比較字符串(或字符串到數字)。這就像是比較if(1 ==「1」)的例子(注意,雙引號只在一邊,因爲左邊是數字,等式的右邊是一個字符串)。

我添加了一個測試,它應該強制這兩個值都是字符串,然後比較它們。如果問題仍然存在,請確保沒有將空格/選項卡添加到一個變量,但在其他變量中缺失。

此外,我改變了你的「警報」輸出到控制檯(CTRL + SHIFT + J,如果你使用的是Firefox)。使用警報的問題有時遠程數據在需要時不可用,但當數據正在讀取時,警報按鈕會創建暫停。所以......如果你使用alert,你的代碼工作,然後你刪除alert,你的代碼可能會顯示新的錯誤(因爲遠程數據沒有按時提供)。現在可能不是問題,但可能是您前進的一個問題。

祝你好運!

function ArticlesListReturn(returned) { 
    xml = returned.documentElement; 
    var rel = document.getElementById('related_category_articles'); 
    rel.options.length = 0; 
    var status = getXMLData('status'); 
    var title = ''; 
    var id = ''; 
    var authorid = ''; 


    if (status == 0) { 
     alert("%%LNG_jsArticleListError%%" + errormsg); 
    } else { 
     var authorid = document.getElementById("authorid").value; // Serge 

     // authorsid = getNextXMLData('authors',x); 
     for (var x = 0; x < xml.getElementsByTagName('titles').length; x++) { 


     title = getNextXMLData('titles', x); 
     id = getNextXMLData('ids', x); 
     authorsid = getNextXMLData('authors', x); 

     console.log("authorsid = "+authorsid); // authors of each article - it returns the proper values 
     console.log("authorid = "+authorid); // author of the page we are on - it returns the proper value 


if(authorsid.toString() == authorid.toString()) 
{ 

     rel.options 
     var count = 0; 
console.log(authorsid.toString()+" equals "+authorid.toString()); 
     rel.options[rel.options.length] = new Option(title, id, authorid); // lign that returns results 

}  
else 
{ 
    console.log(authorsid.toString()+" NOT equals "+authorid.toString()); 
} 


     title = ''; 
     id = ''; 
     authorid = ''; 

     } 

你是否檢查控制檯的消息?它是否正確顯示authorid和authorsid?

我已編輯的腳本,並提出了幾個新增的......

  1. 控制檯會告訴你,如果條件檢查工作或沒有(這意味着你會得到每個記錄的消息)。請參閱我添加的「if/else」和額外的「console.log」部分?

  2. rel.options [x]更改爲等於rel.options [rel.options.length]。我很好奇你爲什麼設置rel.options.length = 0,而我會完成rel.options = new Array();

+0

感謝您的建議。您的解決方案返回空列表。我正試圖弄清楚。 – Sergelie

+0

是的,我做過了,它只返回一次(authorid和authorsid),其餘的都返回給authorid,併爲authorid留下空白。總體結果又是一個空白列表。這是你最近的變化。 所以它看起來像if語句不適用於每篇文章。 – Sergelie

+1

if(authorsid.toString()== autorid)< - 這個小改動使fjprojects解決方案工作!謝謝大家! – Sergelie