简体   繁体   English

从ajax成功更改多个HTML元素

[英]Change multiple HTML element from ajax success

I have an ajax on success function where I wrote some css color changing task upon satisfying some conditions.I'm changing the html element's color using "document.getElementById" .我有一个关于成功功能的 ajax,我在满足某些条件时编写了一些 css 颜色更改任务。我正在使用"document.getElementById"更改 html 元素的颜色。 My problem is that there should me more than one data object where html element should be change but in that case its always the last object.我的问题是,应该有多个数据对象,其中 html 元素应该更改,但在这种情况下,它始终是最后一个对象。

My on success function:我的成功功能:

            success: function (data) {
            $.each(data, function (index, value) {
                if (userId == value.ApplicationUserId && postId == value.PostId && value.IsLiked == 1) {
                    document.getElementById(likeId).style.color = "red";
                }
                if (userId == value.ApplicationUserId && postId == value.PostId && value.IsLiked == 2) {
                    document.getElementById(dislikeId).style.color = "red";
                }
                if (userId == value.ApplicationUserId && postId == value.PostId && value.IsHelpfull == 1) {
                    document.getElementById(helpfulId).style.color = "red";
                }
            });
        }

And my element ID's are all unique and dynamically generated.Also I'm passing the right Id inside documet.getElementById.而且我的元素 ID 都是唯一的并且是动态生成的。此外,我还在 documet.getElementById 中传递了正确的 ID。 How can I change all elements satisfying the conditions ?如何更改满足条件的所有元素?

Given your clarification in the comments it seems you want something like this.鉴于您在评论中的澄清,您似乎想要这样的东西。

First, since the first two conditions of all of your conditionals are the same, you can check those once and nest the other checks within it.首先,由于所有条件的前两个条件都相同,因此您可以检查一次并在其中嵌套其他检查。

if (userId == value.ApplicationUserId && postId == value.PostId) {
    ...
}

Then, you have two separate cases to check:然后,您需要检查两个单独的案例:

  1. If it's liked or disliked.如果喜欢或不喜欢。 Since these are mutually exclusive you can combine them with an else .由于这些是互斥的,您可以将它们与else结合使用。
if (value.IsLiked == 1) {
    document.getElementById(likeId).style.color = "green"; // Liked color
} else if (value.IsLiked == 2) {
    document.getElementById(dislikeId).style.color = "red"; // Disliked color
}
  1. If it's helpful如果有帮助
if (value.IsHelpfull == 1) {
  document.getElementById(helpfulId).style.color = "blue"; // Helpful color
}

success: function (data) {
  $.each(data, function (index, value) {
    if (userId == value.ApplicationUserId && postId == value.PostId) {

      if (value.IsLiked == 1) {
        document.getElementById(likeId).style.color = "green";
      } else if (value.IsLiked == 2) {
        document.getElementById(dislikeId).style.color = "red";
      }

      if (value.IsHelpfull == 1) {
        document.getElementById(helpfulId).style.color = "blue";
      }

    }
  });
}

ps: check that your spelling of helpful is consistent and correct throughout. ps:检查您对helpful的拼写是否始终一致且正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM