简体   繁体   English

.Click函数在jQuery中不起作用

[英]A .Click function not working in jQuery

What I need: 我需要的:

I need the button2 to appear when button1 is clicked. 我需要在单击button1时显示button2。 NEW STUFF: What I need it to do is save button2 there so if you refresh or "run" button 2 is already there how would I do that? 新材料:我需要做的是将button2保存在那里,所以如果您刷新或“运行”按钮2已经在那里,我该怎么做?

What I've done/Tried: 我已经完成/尝试过的事情:

I've tried a bunch of different things such as changing it around changing the scope, as well as checking for console errors, I get none so I'm not too sure what is going on. 我尝试了很多不同的事情,例如在更改范围周围进行了更改,以及检查控制台错误,但没有得到任何信息,所以我不太确定这是怎么回事。

CODE

$(document).ready(function () {
  $('#button2').hide();
  window.highestLevel = 1;

$('#button1').click(function () {
    Check();
    highestLevel = 2;
});

  if (highestLevel == 2) {
    $('#button2').show();
}

function Check() {
    localStorage.setItem('highestLevel', highestLevel);
  }

});

WHAT WORKS 有什么用

The only thing that works is it stores it correctly to the html local storage when I click $('#button1') . 唯一起作用的是,当我单击$('#button1')时,它将正确地存储到html本地存储中。

Link , jsFiddle 链接jsFiddle

For this to work as you describe, the if-structure ... 为了使它如您所描述的那样起作用,if结构...

if (highestLevel == 2) {
    $('#button2').show();
}

... must be inside the click function. ...必须位于click函数中。 Try ... 尝试...

$('#button1').click(function () {
    Check();
    highestLevel = 2;
    $('#button2').show();
});

You really don't need the if-structure doing it this way ... in your code, the if-structure only runs on document-ready (ONE TIME). 您确实不需要用这种方式的if-结构...在您的代码中,if-结构仅在文档就绪(一次)上运行。

You are mistaken about what is happening. 您对正在发生的事情感到误解。 This code block runs when the document is loaded, not after button1 is clicked. 此代码块在加载文档时运行,而不是在单击button1之后运行。

You want to put it inside the click function 您想将其放在点击功能中

$('#button1').click(function () {
  Check();
  highestLevel = 2;

  if (highestLevel == 2) {
    $('#button2').show();
  }
})

You will also want to prevent the default action of the button, which is to post the page causing it to reload, by adding an event object 您还将希望通过添加事件对象来阻止按钮的默认操作,该默认操作是发布导致其重新加载的页面

$('#button1').click(function (e) {
  e.preventDefault();
  Check();
  highestLevel = 2;

  if (highestLevel == 2) {
    $('#button2').show();
  }
})


});

You don't need all of that... The problem is that 您不需要所有这些...问题是

if (highestLevel == 2) {
    $('#button2').show();
}

executes only once, when the document is ready. 文档准备好后,仅执行一次。

Try this: 尝试这个:

$(document).ready(function () {
  $('#button2').hide();

  $('#button1').click(function () {
     $('#button2').show();
  });
});

Try with this 试试这个

        window.highestLevel = localStorage.getItem('highestLevel');
        $(document).ready(function () {
            if (highestLevel == 2) {
                $('#button2').show();
                $('#button1').hide();
            }
            else {
                highestLevel == 1
                $('#button1').show();
                $('#button2').hide();
            }

            $('div').click(function () {
                if (highestLevel == 2) {
                     highestLevel = 1;
                    $('#button1').show();
                    $('#button2').hide();
                }
                else{
                    highestLevel = 2;
                    $('#button2').show();
                    $('#button1').hide();
                }
                Check();
            });
        });


        function Check() {
            localStorage.setItem('highestLevel', highestLevel);
        }

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

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