gpt4 book ai didi

javascript - 单击时 HTML/Javascript/Bootstrap 无法隐藏按钮

转载 作者:行者123 更新时间:2023-11-28 01:26:21 24 4
gpt4 key购买 nike

所以我有这段代码,我试图让登录/注册按钮在单击后隐藏按钮,它们会闪烁(隐藏并快速返回)。

我是 web 开发的新手,所以我可能会做一些愚蠢的事情

<!DOCTYPE html>

<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<!--CSS-->
<link rel="stylesheet" href="Login.css" />

<meta charset="utf-8" />
</head>
<body>
<div class="jumbotron">
<!--Login/Register Tabs-->
<ul class="nav nav-tabs nav-justified">
<li class="active"><a data-toggle="tab" href="#loginTab">Login</a></li>
<li><a data-toggle="tab" href="#registerTab">Register</a></li>
</ul>
<!--Tabs Content-->
<div class="tab-content">
<div id="loginTab" class="tab-pane active">
<form>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input id="loginUsernameText" type="text" class="form-control" placeholder="Username">
</div>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input id="loginPasswordText" type="password" class="form-control" placeholder="Password">
</div>
<button id="loginButton" class="btn btn-primary btn-block">Login</button>
</form>
</div>

<div id="registerTab" class="tab-pane fade">
<form>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input id="registerUsernameText" type="text" class="form-control" placeholder="Username (3-24 Char)">
</div>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input id="registerPasswordText" type="password" class="form-control" placeholder="Password (3-24 Char)">
</div>
<button id="registerButton" class="btn btn-primary btn-block">Register</button>
</form>
</div>
</div>
</div>

<!--Scripts-->
<script src="Login.js" type="text/javascript"></script>
</body>
</html>

用jquery写的Login.js

$('#loginButton').click(function () {
$('#loginButton').addClass('hidden');
$('#registerButton').addClass('hidden');
});

$('#registerButton').click(function () {
$('#loginButton').addClass('hidden');
$('#registerButton').addClass('hidden');
});

感谢任何帮助

已解决:按钮的设置属性 type="button"救了我的命,感谢您的帮助!

最佳答案

尝试以下操作:

1) 将 type="button"属性添加到您的按钮,可能会避免某些情况下表单中的按钮充当提交,除非声明为 type=button。

2) 改写如下

var registerButton = $('#registerButton');
var loginButton= $('#loginButton');

function hideButtons(){
registerButton.hide(); // or add your custom class here
loginButton.hide(); // or add your custom class here
return false; // avoid bubling
}

loginButton.on('click',function () {
hideButtons();
});

registerButton.on('click',function () {
hideButtons();
});

告诉我它是否有效

关于javascript - 单击时 HTML/Javascript/Bootstrap 无法隐藏按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51225040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com