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

Function to return a list - issue with filtering the output

本文关键字:问题 输出 过滤 列表 函数 返回      更新时间:2023-09-26

如果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 = '';
      }
}

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

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

另外,我更改了您的"警报"以输出到控制台(如果您使用的是 Firefox,则为 CTRL+SHIFT+J)。使用警报的问题是有时远程数据在需要时不可用,但警报按钮会在读取数据时创建暂停。所以。。。如果使用 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 = '';
      }

您是否检查了控制台中的消息?它是否正确显示了作者ID和作者?

我已经编辑了脚本并做了一些补充...

  1. 控制台将告诉您条件检查是否有效(这意味着您将收到每条记录的消息)。看到我添加的"if/else"和额外的"控制台.log"部分了吗?

  2. rel.options[x] 更改为相等的 rel.options[rel.options.length]。我很好奇为什么你设置 rel.options.length=0 当我改为做 rel.options=new Array();