显示模式而不是控制台警报
我正在学习 Javascript 并研究了这些类似的案例1,2,3, 4,5 ,6,7,8没有成功。我不使用 jQuery 或 Bootstrap 等依赖项。
我想在下面的代码中将 alert("Success!"); 替换为 document.getElementById('modal-team').showModal(); ,以显示模式并使用户保持在同一页面中:
window.addEventListener("load", function() {
const form =
document.getElementById('registration');
form.addEventListener("submit", function(e) {
e.preventDefault();
const data = new FormData(form);
const action = e.target.action;
fetch(action, {
method: 'POST',
body: data,
})
.then(() => {
alert("Success!");
})
});
});
您可以帮忙吗?
I am learning Javascript and have studied these similar cases 1, 2, 3, 4, 5, 6, 7, 8 with no success. I don't use dependencies like jQuery or Bootstrap.
I want to replace alert("Success!"); with something like document.getElementById('modal-team').showModal(); in the below code, to show a modal and keep the user in the same page:
window.addEventListener("load", function() {
const form =
document.getElementById('registration');
form.addEventListener("submit", function(e) {
e.preventDefault();
const data = new FormData(form);
const action = e.target.action;
fetch(action, {
method: 'POST',
body: data,
})
.then(() => {
alert("Success!");
})
});
});
Can you please assist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JavaScript 或 HTML 标准中不存在模态。为了定义这个概念,您有两种选择:
上面列出的问题使用 Bootstrap 的模态实现,因此仅适用于您使用 Bootstrap 的情况。
如果没有库,模式的实现并不是特别困难,但是您必须考虑解决一些问题:
>
级别或.modal-body
级别)上述所有问题在大多数模式实现中都已得到解决,这就是为什么人们更喜欢使用它们,而不是编写自己的实现。
要回答您的具体问题,
HTMLDivElement
没有.showModal()
方法。在调用此类方法之前,您首先需要定义它。Modals do not exist in JavaScript or HTML standard. In order to define the concept, you have two options:
The questions listed above are using Bootstrap's implementation of modals and therefore would only be applicable if you used Bootstrap.
A modal is not particularly difficult to implement without a library, but you have to consider solving a few problems:
<body>
level or at.modal-body
level)All of the above are already solved in most modal implementations, which is why people prefer using them, rather than writing their own.
To answer your specific question, an
HTMLDivElement
does not have a.showModal()
method. Before you could call such a method, you first need to define it.我使用不同的路径来解决这个问题,但是使用了更多我不喜欢的代码。
使用相同的js进行拦截(只是删除.then):
为具有唯一id的表单调用onsubmit函数:为
表单提供服务的js函数:
js函数显示的模态(使用对话框,因为现代浏览器支持它 1):
I figure it out using a different path, but using more code which I don't like.
Used the same js to intercept (just removed .then):
Call an onsubmit function for a form with a unique id:
The js function that serves the form:
The modal that is showed by the js function (used the dialog since it's supported by modern browsers 1):
If someone can provide a solution with less code (and for multiple ids), for the same result with no dependencies, please be my guest; I would love to learn.