【问题标题】:jquery empty() not working in an Ajax calljquery empty() 在 Ajax 调用中不起作用
【发布时间】:2020-08-13 10:01:08
【问题描述】:

我有一个模态框,我在其中放置了一些元素

 <a class="incidents" tabindex="0"  id= 'incident0' data-toggle="popover" data-html="true" data-trigger="focus" data-placement="bottom" data-content=""></a>
 <a class="incidents" tabindex="1"  id= 'incident1' data-toggle="popover" data-html="true" data-trigger="focus" data-placement="bottom" data-content=""></a>
 <a class="incidents" tabindex="0"  id= 'incident2' data-toggle="popover" data-html="true" data-trigger="focus" data-placement="bottom" data-content=""></a>
 <a class="incidents" tabindex="0"  id= 'incident3' data-toggle="popover" data-html="true" data-trigger="focus" data-placement="bottom" data-content=""></a>

在完成我的 ajax 调用后,我使用 for 循环动态填充元素,我将获得的最大对象数为 4,因此我将其构造为如果只有 2 个对象,它在事件 2 处停止并爆发。

if (data.incidents.issuesQty != 0) 
   { 
     //console.log('I am here');           
     for (var i = data.incidents.issuesQty - 1; i >= 0; i--) 
     {                  
          if (data.incidents.issues[i].type == "ban")
          {
            $( "#incident"+i ).html("<i class=\"fas fa-ban\"></i>");
            $("#bannedUser" ).html("BANNED");
          }
          if (data.incidents.issues[i].type > 3)
          {
            $( "#incident"+i ).html("<i class=\"fas fa-thermometer-full\"></i>");
          }
          else if(data.incidents.issues[i].type > 2)
          {
            $( "#incident"+i ).html("<i class=\"fas fa-thermometer-half\"></i>");
          }
          else if(data.incidents.issues[i].type < 3)
          {
            $( "#incident"+i ).html("<i class=\"fas fa-thermometer-empty\"></i>");
          }
         $( "#incident"+i ).attr("data-content", '<b>Notes: </b>'+data.incidents.issues[i].notes');
     }
   }

问题是如果我有一个填充元素事件 1 和事件 2 的用户,然后我单击另一个没有事件的用户,即跳过 if 语句块;元素“incident1”仍然会记住前一个用户的数据并仍然显示它。

我尝试在 ajax 成功开始时使用 jQuery 空方法来清除元素,但由于某种原因它仍然会反刍旧数据。我还放置了一个 console.log 调试来验证 if 语句是否被跳过,但我仍然有一个

$( "#incident1").empty();
$( "#incident2").empty();
$( "#incident3").empty();
$( "#incident4").empty();

【问题讨论】:

  • 如果没有看到问题的模拟示例,很难遵循逻辑流程来帮助您。但是我可以看到有语法错误;您在最终的字符串连接中缺少 + 。检查问题中突出显示的语法以发现它。

标签: javascript html jquery laravel


【解决方案1】:

当然,我犯了从 '1' 而不是从 '0' 开始计数的所有时间新手错误。我的元素索引从 0 到 3,但我试图从 1 清空到 4,这意味着元素 0 始终保持不变!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多