jquery - jQuery,消失的元素
问题描述
$(document).ready(function() {
$("#lastinput").click(function() {
$("#blank").html("text to display");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="jquery.min.js"></script>
</head>
<body>
<div id="blank"> </div>
<div id="login">
<form>
<label for="email">Email</label>
<input type="text" name="email" placeholder="eg. yourname@gmail.com" id="email"><br>
<label for="phone">Telephone</label>
<input type="text" name="phone" placeholder="eg. 123456789" id="phone"><br>
<label for="password">Password</label>
<input type="password" name="password" placeholder="password" id="password"><br><br>
<input id="lastinput" type="submit" value="Log in">
</form>
</div>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
我有一个名为 id = "blank" 的显示元素的问题浏览器在 0.1 秒上显示它然后消失,我搜索了一天的解决方案,仍然找不到错误提前感谢您的帮助。
解决方案
您的提交按钮位于表单内,因此一旦您提交该表单,它就会返回一个 url 以重定向到。由于您没有提供一个,它会重定向到一个空白页面。
您需要从表单中删除提交按钮或将表单转换为 Ajax 表单以提交信息并更新页面而不刷新它:
$("#lastinput").click(function(event) {
$.ajax({
url: 'url here',
beforeSend: function () {
$("#blank").html("text to display");
},
success: function() {},
}
});
});
推荐阅读
- c# - 是否有 C# 速记将值转换为其方法的返回类型?
- django - Django inspectdb 无法检查表 - 关系不存在错误
- java - 将两个对象创建到一个数组中
- google-cloud-firestore - Flutter Firestore ld:未找到架构 x86_64 的符号
- c# - 使用 Entity Framework Core 显示来自 2 个链接表的信息时出错
- php - 迄今为止的 PHP UTC 字符串
- javascript - chartjs 雷达发光边缘效果
- python - 读取没有代表行尾的字符的 txt 文件。如何遍历行
- c# - 不知道为什么这个单选按钮会引发错误?
- java - 我试图弄清楚如何打印我排序的这个数组列表