简体   繁体   English

如何获取.click按钮以隐藏警报框

[英]How to get a .click button to hide an alert box

I have been creating a page out of json, and there is a simple log in. When the user ID and name matches that of my json file: 我一直在用json创建页面,并且有一个简单的登录信息。当用户ID和名称与我的json文件的名称匹配时:

{
"user":[
{
"ID" : "001",
"imgpath":"image/zara.jpg",
 "message": " , We have new iphones avaiable",
 "name": "Zara Ali"

 },
 {
"ID" : "002",
"imgpath":"image/iphone5.jpg",
"prefer": "  Loves macs",
"name": "Laura Ali"
},
{
"ID" : "003",
"imgpath":"image/iphone5.jpg",
"message": "  Loves ipods",
"name": "Courtney Ali"
 },
 {
"ID" : "004",
"imgpath":"image/iphone5.jpg",
"message": "  Loves Ipads",
"name": "Max Ali"
}
]
}

So the name is almost like user name and the id is like password, when these match an alert pops up at the top of page containing the user's picture, name etc and if an invalid user tries to enter their details(not part of the json or username and password does not match an invalid alert appears) I also have a hide button that appears welcome user alert, i have it included but when i click it and try to add a .click hide() function this will not work. 因此,名称几乎就像用户名,而id就像密码,当这些匹配时,在页面顶部弹出一个警报,其中包含用户的图片,姓名等,并且如果无效的用户尝试输入其详细信息(不是json的一部分)或出现用户名和密码不匹配的无效警报)我还有一个隐藏按钮,其中显示欢迎用户警报,我已将其包含在内,但是当我单击它并尝试添加.click hide()函数时,它将不起作用。

This is my script so far including the button, in the script you will see this as part of the login alert: 到目前为止,这是我的脚本,包括按钮,在脚本中,您将看到此作为登录警报的一部分:

<button type="button" id="btnhide" class="btn btn-primary btn-md">Hide</button>

:

$(document).ready(function() {
//Hide alert when page loads
$("#loginalert").hide();
$("#invalid").hide();  
$("#loginbtn").click(function(event){
//console.log("clicked login");
$.getJSON('result.json', function(jd) {
  var id = $('#userName').val();
  var name = $('#userName2').val();
  var valid = false;
  //console.log(id);
  for (var i=0; i<jd.user.length; i++) {
    if ((jd.user[i].ID == id) && (jd.user[i].name == name)) {
        valid=true;
      $('#loginalert').html('<img src="' + jd.user[i].imgpath + '"><br><p> Welcome: ' + jd.user[i].name + '</p><button type="button" id="btnhide" class="btn btn-primary btn-md">Hide</button>');      
      //show the alert after loading the information  
        $("#loginalert").stop().fadeIn('slow').animate({ opacity: 1.0 }, 3000)('slow', function () {
        //$("#invalid").hide();

    });
    }
  }
 if (!valid) {
    $('#invalid').fadeIn('slow');

 }

  }); });  });

So i need to make the button actually hide the #loginalert 所以我需要使按钮实际上隐藏#loginalert

many thanks guys 非常感谢你们

You can just delegate event: 您可以委托事件:

$('#loginalert').on('click', '#btnhide', function(e){
    $(e.delegateTarget).hide(); // or `$('#loginalert').hide()`
});

I got this to work.. please see below: 我有这个工作..请看下面:

$("#loginalert").stop().fadeIn('slow').animate({ opacity: 1.0 }, 3000)
                $('#invalid').hide();
                $('#btnhide').on('click', function(e){

                    e.preventDefault();
                    $('#loginalert').hide();
                });

            }
        }
        if (!valid) {
            $('#invalid').fadeIn('slow');
            $('#loginalert').hide();

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

相关问题 如何在单击按钮时获得警报消息 - How to get alert message on second click of a button 单击“提交”按钮时如何在JavaScript警报中回显复选框 - How to echo Check Box in JavaScript Alert When Click on Submit Button 如何在按钮单击后的代码中调用警报框? - how to call an alert box in code behind on button click in? 如何在JavaScript中隐藏警报框 - How to hide alert box in javascript 单击按钮时输入框的警报内容-React - Alert content of input box on click of button — React 当我单击“计算”按钮时,它如何获取每个A,B框中的值,并将这些值传递给服务器以计算结果并发出Alert()? - When I click the “Calculate” button, how can it get the values in each box A,B and pass those value to server to calculate and alert() the result? 单击按钮时显示 Bootstrap 警报框 - Show Bootstrap alert box on a button click 如何在单选按钮单击事件上隐藏/显示输入框 - how to hide/ show input box on radio button click event 在 React 中,单击按钮时,显示警报 5 秒,然后隐藏警报 - In React, on button click, display alert for 5 seconds, then hide alert 如何在没有消息时隐藏Bootstrap警报框 - How to hide Bootstrap alert box when no message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM