javascript - Popovers dissapear on 'onClick' event
问题描述
I've implemented few popovers and I try to trigger them thourgh a function which is called by onclick event. I have no idea why, but they dissappear in a second.I actually managed to get them work fine when checking each one, but when user press "submit" button, it disappers. any idea how and HTML:
function fieldValidation(textBox){
if (textBox.value === "" || textBox === "") {
textBox.style.borderColor = "red";
$(textBox).popover('show');
}
else {
$(textBox).popover('hide');
textBox.style.borderColor = "green";
}
}
function formValidation(){
var fields = document.getElementsByClassName("personal").length;
var current, check = true;
for (var index = 0; index < fields ; index++) {
current = document.getElementsByClassName("personal")[index];
if(current.value === "") {
fieldValidation(current);
check = false;
}
}
if (!check)
return;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Academic Calculator</title>
<meta name="Calculator" content="Academic Calculator">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="Libs/bootstrap.css">
<script src="Libs/http_code.jquery.com_jquery-2.0.0.js"></script>
<script src="Libs/bootstrap.js"></script>
<link rel="stylesheet" href="Calculate.css">
</head>
<body>
<script src="calculatorLogic.js"></script>
<div><h1>Academic Calculator</h1></div>
<div class="container-fluid">
<hr>
<div class="row">
<form>
<div class ="col-lg-1 col-md-2 col-sm-2 col-xs-12" >
<input class="personal" placeholder="First Name:" data-toggle="popover"
data-content="Please Fill First Name Correctly" onblur="fieldValidation(this)"
data-placement="top">
</div>
<div class ="col-lg-1 col-md-2 col-sm-2 col-xs-12">
<input class="personal" placeholder="Last Name:" data-toggle="popover"
data-content="Please Fill Last Name Correctly" onblur="fieldValidation(this)"
data-placement="top">
</div>
<div class = "col-lg-1 col-md-2 col-sm-2 col-xs-12">
<input class="personal" placeholder="Academic Institute:" data-trigger="popover"
data-content="Please Fill Academic Institute Correctly" onblur="fieldValidation(this)"
data-placement="top">
</div>
<div class = "col-lg-1 col-md-2 col-sm-2 col-xs-12">
<input class="personal" placeholder="School:" data-trigger="popover"
data-content="Please School Institute Correctly" onblur="fieldValidation(this)"
data-placement="top">
</div>
<div class = "col-lg-1 col-md-2 col-sm-2 col-xs-12 start-btn">
<input type="submit" class="submit" onclick="formValidation()">
</div>
</form>
</div>
<hr>
<div class="courses"></div>
</div>
</body>
</html>
解决方案
如果您不需要该功能,请不要使用 type="submit"。
您应该能够将类型更改为“按钮”并通过在输入元素上定义“值”属性来添加按钮标签。
<input type="button" value="submit" class="submit" onclick="formValidation()" />
如果您使用此按钮提交表单但不想要默认功能,那么您始终可以使用“preventDefault()”
<input onclick="myFunc(e)" type="submit" />
function myFunc(e){
e.preventDefault();
}
以下是一些有助于进一步研究的链接: https ://www.w3.org/TR/html401/interact/forms.html#edef-BUTTON
推荐阅读
- java - Java swing如何在另一个线程中打开框架
- python - Pandas 按类别用均值替换负数
- ruby-on-rails - 尝试在 Rails 中迁移时出现连接错误。
- ruby-on-rails - 黄瓜功能重定向失败
- vb.net - 在 VB 中出现“不是成员”错误,但可以在 Visual Studio 调试器中看到该属性
- ios - cocoapods 的错误:无法添加源
- serverless-framework - 如何在无服务器框架中实现步进函数的别名
- r - 具有附加功能的预测时间序列
- mysql - 使用 TCP 连接到 MySQL
- visual-studio - VS中的选项卡顺序工具 - 无法通过标签查看控件