2016-02-28 101 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