返回列表的函数 - 过滤输出的问题

时间:2016-02-28 16:50:01

标签: javascript if-statement

我正在尝试仅输出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 = '';

      }
}

1 个答案:

答案 0 :(得分:0)

我怀疑问题是当你尝试执行一个条件语句(if / then / else)时,你正在将一个数字与一个字符串(或一个字符串与一个数字)进行比较。这就像比较if(1 ==“1”)例如(注意双引号仅在一侧,因为左边是数字,等式的右边是字符串)。

我添加了一个测试,它应该强制两个值都是字符串,然后比较它们。如果它仍然给你带来问题,请确保没有空格/制表符添加到一个变量,但在另一个变量中没有。

此外,我更改了“警报”以输出到控制台(如果您使用的是Firefox,则按CTRL + SHIFT + J)。使用警报的问题有时在需要时远程数据不可用,但是在读取数据时,警报按钮会产生暂停。所以...如果您使用警报,您的代码可以工作,那么您删除警报,您的代码可能会显示新的错误(因为远程数据未按时提供)。它现在可能不是问题,但对你来说可能是一个问题。

祝你好运!

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();