简体   繁体   English

在 HTML 中使用模态弹出窗口并返回上一页

[英]Using modal pop-ups in HTML and returning to previous page

I'm learning HTML, CSS, JavaScript and SQL.我正在学习 HTML、CSS、JavaScript 和 Z9778840A0100CB30C582876741B。

The following info is "context"以下信息是“上下文”

The scenario is a website where I can add information about dogs, information about owners of the dogs and link the dogs to the owners.该场景是一个网站,我可以在其中添加有关狗的信息、有关狗主人的信息并将狗链接到主人。 The data is stored in an SQL database.数据存储在 SQL 数据库中。

I can successfully add a record about a dog and an owner and link them by having the OwnerTable primary key as a foreign key in the DogTable.我可以成功地添加一条关于狗和主人的记录,并通过将 OwnerTable 主键作为 DogTable 中的外键来链接它们。 All good.都好。

It is required to be able to create a dog record with initially no owner then add the owner later.需要能够创建最初没有所有者的狗记录,然后再添加所有者。 OR as I add a dog record I might assign it to an existing owner in the database.或者,当我添加一条狗记录时,我可能会将其分配给数据库中的现有所有者。

Say I am adding a dog record then realise I want to link to an owner not in the database.假设我正在添加一条狗记录,然后意识到我想链接到不在数据库中的所有者。

Now the problem/ question.现在是问题/问题。

I can add a button to the add dog page which will allow me to add a new owner, while "in the middle" of adding a new dog record.我可以在添加狗页面上添加一个按钮,这将允许我添加一个新主人,同时“中间”添加一条新的狗记录。 It seems to me from what I have read there are two approaches:在我看来,从我所读到的有两种方法:

  1. Just switch to the existing "add owner" page using href只需使用 href 切换到现有的“添加所有者”页面
  2. Create a modal pop-up (correct term?) when I click the add owner button当我单击添加所有者按钮时,创建一个模式弹出窗口(正确的术语?)

Now 1. seems less desirable because I would have to navigate back to the add dog page after adding a new owner.现在 1. 似乎不太理想,因为在添加新所有者后我必须导航回添加狗页面。

  1. Seems more attractive because the workflow isn't interrupted, it is like the user experience is "I need to add a dog but I need to add an owner. I will quickly add an owner and then continue with adding the dog without losing the information I have already typed about the dog"看起来更有吸引力,因为工作流程没有中断,就像用户体验是“我需要添加狗,但我需要添加主人。我会快速添加主人,然后继续添加狗,而不会丢失信息我已经输入了关于狗的信息”

However, the problem I anticipate with 2 is I will have two instances of code that do the same thing - a "standalone" web page for adding an owner and a modal popup for adding an owner.但是,我预计 2 的问题是我将有两个执行相同操作的代码实例 - 一个用于添加所有者的“独立”web 页面和一个用于添加所有者的模式弹出窗口。 My concern is there will be duplicated code across the page and the modal - which will lead to a maintenance issue later should I need to change the "add owner" code in some way.我担心页面和模式中会有重复的代码 - 如果我需要以某种方式更改“添加所有者”代码,这将导致稍后出现维护问题。

Is there a way round this duplication - or other HTML/JS facilities I could use - but still retain the ability to (1) add an owner "standalone" or (2) add an owner while adding a new dog record?有没有办法绕过这种重复 - 或我可以使用的其他 HTML/JS 工具 - 但仍然保留(1)添加“独立”所有者或(2)在添加新狗记录时添加所有者的能力?

You can use the same request end point for both the add owner page and the modal version.您可以为添加所有者页面和模式版本使用相同的请求端点。 The only thing you'd need to maintain is the add owner page.您唯一需要维护的是添加所有者页面。 If you aren't using any server side tech, I think the easiest way would be to place an iframe inside the modal so that you can use the same html file for both the modal and the add owner page.如果您不使用任何服务器端技术,我认为最简单的方法是将iframe放置在模态中,以便您可以对模态和添加所有者页面使用相同的html文件。 This post has some more information on doing what Asp.Net calls a partial view with just vanilla javascript - Partial render in HTML/JavaScript这篇文章有更多关于做Asp.Net所谓的部分partial view的信息

Be warned that using the pure javascript method is prone to failure if someone has javascript disabled in their browser.请注意,如果有人在浏览器中禁用了javascript ,则使用纯javascript方法很容易失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM