显示模式而不是控制台警报

发布于 2025-01-14 17:11:57 字数 1504 浏览 3 评论 0原文

我正在学习 Javascript 并研究了这些类似的案例123, 45678没有成功。我不使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等你爱我 2025-01-21 17:11:57

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:

  • you create it yourself
  • you load one particular implementation from a library which defines it in a particular way.

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:

  • how it displays on all devices, including mobile devices
  • how it scrolls when its contents exceed the screen size (if you implement scrolling at <body> level or at .modal-body level)
  • how you handle multiple modal instances (when you open a modal from another modal, do you keep them both open or do you close the initial modal)
  • if (and how) you allow data injection into the modal instance and if (and how) you allow data injection back from the modal instance, when the modal gets closed (typically solved by specifying a callback which gets called by the modal, before it closes).

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.

笑咖 2025-01-21 17:11:57

我使用不同的路径来解决这个问题,但是使用了更多我不喜欢的代码

使用相同的js进行拦截(只是删除.then):

window.addEventListener("load", function() {
const t = document.getElementById("infobar");
t.addEventListener("submit", function(e) {
e.preventDefault();
const n = new FormData(t),
o = e.target.action;
fetch(o, {
method: "POST",
body: data, }) }) });

为具有唯一id的表单调用onsubmit函数:为

<form onsubmit="Dia()" id="infobar" name="form" 
method="post" action="blablabla">
<input type="email" name="Email" placeholder="Email here">
<input type="submit" value="Join"></form>

表单提供服务的js函数:

function Dia(){document.getElementById("Di").showModal()};

js函数显示的模态(使用对话框,因为现代浏览器支持它 1):

<dialog id="Di">

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):

window.addEventListener("load", function() {
const t = document.getElementById("infobar");
t.addEventListener("submit", function(e) {
e.preventDefault();
const n = new FormData(t),
o = e.target.action;
fetch(o, {
method: "POST",
body: data, }) }) });

Call an onsubmit function for a form with a unique id:

<form onsubmit="Dia()" id="infobar" name="form" 
method="post" action="blablabla">
<input type="email" name="Email" placeholder="Email here">
<input type="submit" value="Join"></form>

The js function that serves the form:

function Dia(){document.getElementById("Di").showModal()};

The modal that is showed by the js function (used the dialog since it's supported by modern browsers 1):

<dialog id="Di">???????? Received.</dialog>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文