即使值为false,事件也会运行

时间:2016-01-03 16:46:55

标签: jquery ajax get

所以,问题是:即使loadedFirst值为false,也会发生这种情况。参见:

var lastA = $(".feedpost-pages > a").last();
var pageToLoad = lastA.attr("href");
var lodadedFirst = false;

$(".loadold").click(function () {
  if (loadedFirst = true) {
    lastA = lastA.prev();
    var pageToLoad = lastA.attr("href");
    alert("this is happening, but the loadedFirst value is false. How?");
  }
  $.get(pageToLoad, function (data) {
    var postsContent = $(data).find(".post:not(:first)");
    $(".feedpost-lastreplys").prepend(postsContent);
    FormatPosts();
    FixPosts();
    countPosts();
    loadedFirst = true;
  });
});

2 个答案:

答案 0 :(得分:1)

原因是:

var lodadedFirst = false
//----^ Remove the d.

第二个是:

if (loadedFirst = true) {
//--------------^ You are assigning and not comparing.

<强>最终

var lastA = $(".feedpost-pages > a").last();
var pageToLoad = lastA.attr("href");
var loadedFirst = false;

$(".loadold").click(function() {
  if (loadedFirst == true) {
    lastA = lastA.prev();
    var pageToLoad = lastA.attr("href");
    alert("this is happening, but the loadedFirst value is false. How?");
  }
  $.get(pageToLoad, function(data) {
    var postsContent = $(data).find(".post:not(:first)");
    $(".feedpost-lastreplys").prepend(postsContent);
    FormatPosts();
    FixPosts();
    countPosts();
    loadedFirst = true;
  });
});

答案 1 :(得分:0)

更改

if (loadedFirst = true)

if (loadedFirst == true)

您要分配而不是比较。

相关问题