简体   繁体   English

Javascript变量本身会更改值

[英]Javascript variable changes value itself

So, I am writing a code for my webpage that if you have one of two boxes open (registration, login) you can't open another one. 因此,我正在为我的网页编写代码,如果您打开了两个框之一(注册,登录),则无法打开另一个框。 I'm stuck with the following problem: once you close the box with pressing the mouse out of the box, value changes to 1 and you should be able to open the box again but all of a sudden it sets itself to 0 and you can't open the box anymore. 我遇到了以下问题:一旦您将鼠标从包装盒中按下来关闭包装盒,值将更改为1,并且您应该能够再次打开包装盒,但是突然之间,它将自身设置为0,您可以不再打开盒子了。

IF you close the box with CLOSE button, you CAN open the box again but not with pressing outside the box. 如果使用“关闭”按钮关闭包装盒,则可以再次打开包装盒,但不能通过在包装盒外面按下来打开。

At the start of JS file I declare var openable and set value to 1. 在JS文件的开头,我声明var openable并将其值设置为1。

var openable = 1; 

Code for closing with CLOSE button: 使用CLOSE按钮关闭代码:

$("#closeReg").click(function(){
    openable = 1;
    console.log(openable);
    $('#register').css('display','none');
    $("#register").siblings().css("opacity", "1");
});

Code for pressing out of box: 开箱即用的代码:

var subject = $("#register");
if(e.target.id == subject.attr('id')){
    subject.fadeOut();
    subject.siblings().css("opacity", "1");
    openable = 1;
    console.log(openable);  
} 

Code for opening the box: 打开盒子的代码:

$("#regButton").click(function(){
    console.log(openable);
    if(openable == 1){
    $("#register").fadeIn();
    $("#register").siblings().css("opacity", "0.5");
    openable = 0;
    console.log(openable);
    }
});

Whole code: https://pastebin.com/vC461VGy 整个代码: https : //pastebin.com/vC461VGy

 var openable = 1; $("#closeReg").click(function() { openable = 1; console.log(openable); $('#register').css('display', 'none'); $("#register").siblings().css("opacity", "1"); }); $("#regButton").click(function() { console.log(openable); if (openable == 1) { $("#register").fadeIn(); $("#register").siblings().css("opacity", "0.5"); openable = 0; console.log(openable); } }); $('body').click(function(e) { if ($(e.target).closest('#register').length <= 0 && !$(e.target).is('#closeReg') && !$(e.target).is('#regButton')) { $('#closeReg').trigger('click'); } }); 
 #register { background-color: #FFAAAA; } body { height: 200px } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="closeReg">Close</button> <button id="regButton">Open</button> <div id="register"> register box </div> 

Here, try this sample. 在这里,尝试此示例。

Your code for hiding when clicking outside was not working correctly, mainly because e is undefined and even if we consider it to be a click event your test was wrong. 您在外部单击时隐藏的代码无法正常工作,主要是因为e未定义,即使我们认为这是单击事件,您的测试也有误。

The main difficulty is to detect the click outside the box without detecting the click that opens it, or you will end up opening and closing at the same time. 主要困难是无法检测到框外的点击,而无法检测到打开框的点击,否则最终会同时打开和关闭。 Hence why I added the two tests to make sure the targets aren't buttons that handle specific behaviors. 因此,为什么我添加两个测试以确保目标不是处理特定行为的按钮。

Also, instead of doubling the code to close the modal, I have made it so when you click anywhere, it triggers a click on the close button, that way only one event is to be kept up to date. 另外,我没有使代码加倍以关闭模式,而是使它在您单击任何位置时都触发​​了对关闭按钮的单击,这样仅一个事件就可以保持最新状态。 Note that this could also be do-able by creating your own event instead but meh... 请注意,这也可以通过创建您自己的事件来实现,但是...

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

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