gpt4 book ai didi

javascript - HTML 表单提交弹出窗口

转载 作者:太空宇宙 更新时间:2023-11-04 00:46:15 25 4
gpt4 key购买 nike

我的网站上有一个 HTML 表单。当我填写它并单击提交时,我的操作端点工作正常。我想用它做以下事情。单击提交时:

  1. 将表单数据发布到操作端点
  2. 用按钮覆盖感谢消息,或者可能只是一条在几秒钟后消失的消息
  3. 一旦覆盖消失,我希望表单被清除,但用户返回到同一页面。

我的基本形式是这样的:

<form method="post" action="https://mywebsite.com/formdata">
<div class="fields">
<div class="field half">
<input type="text" name="name" id="name" placeholder="Name" />
</div>
<div class="field half">
<input type="email" name="email" id="email" placeholder="Email" />
</div>
<div class="field">
<textarea name="message" id="message" placeholder="Message"></textarea>
</div>
</div>
<ul class="actions">
<li><input type="submit" value="Send" class="primary" /></li>
</ul>

我尝试通过从我发现的一些在线指南中添加覆盖代码来修改代码,如下所示:

<form method="post" action="https://mywebsite.com/formdata">
<div class="fields">
<div class="field half">
<input type="text" name="name" id="name" placeholder="Name" />
</div>
<div class="field half">
<input type="email" name="email" id="email" placeholder="Email" />
</div>
<div class="field">
<textarea name="message" id="message" placeholder="Message"></textarea>
</div>
</div>
<ul class="actions">
<li><input class="trigger_popup_fricc" type="submit" value="Send" class="primary" /></li>
</ul>
<div class="hover_bkgr_fricc">
<span class="helper"></span>
<div>
<div class="popupCloseButton">X</div>
<p>Add any HTML content<br />inside the popup box!</p>
</div>
</div>

添加了在提交按钮上添加的类及其下方的悬停弹出 div。然后添加的 CSS 是:

    /* Popup box BEGIN */
.hover_bkgr_fricc{
background:rgba(0,0,0,.4);
cursor:pointer;
display:none;
height:100%;
position:fixed;
text-align:center;
top:0;
width:100%;
z-index:10000;
}
.hover_bkgr_fricc .helper{
display:inline-block;
height:100%;
vertical-align:middle;
}
.hover_bkgr_fricc > div {
background-color: #fff;
box-shadow: 10px 10px 60px #555;
display: inline-block;
height: auto;
max-width: 551px;
min-height: 100px;
vertical-align: middle;
width: 60%;
position: relative;
border-radius: 8px;
padding: 15px 5%;
}
.popupCloseButton {
background-color: #fff;
border: 3px solid #999;
border-radius: 50px;
cursor: pointer;
display: inline-block;
font-family: arial;
font-weight: bold;
position: absolute;
top: -20px;
right: -20px;
font-size: 25px;
line-height: 30px;
width: 30px;
height: 30px;
text-align: center;
}
.popupCloseButton:hover {
background-color: #ccc;
}
.trigger_popup_fricc {
cursor: pointer;
font-size: 20px;
margin: 20px;
display: inline-block;
font-weight: bold;
}
/* Popup box BEGIN */

添加的 javascript 是:

    $(window).load(function () {
$(".trigger_popup_fricc").click(function(){
$('.hover_bkgr_fricc').show();
});
$('.hover_bkgr_fricc').click(function(){
$('.hover_bkgr_fricc').hide();
});
$('.popupCloseButton').click(function(){
$('.hover_bkgr_fricc').hide();
});
});

但它不起作用。没有弹出窗口。非常感谢任何有关如何完成我想完成的任务的指导。

最佳答案

您只需要向 url enpoint 添加一个 ajax 请求并获取响应。但是正如在另一个答案中提到的,您需要使用 preventDefault() 来避免在单击发送按钮时刷新。

这是相同的 jsfiddle - https://jsfiddle.net/ekLys4qa/1/

$(".trigger_popup_fricc").click(function(e){
e.preventDefault();
var my_inputs = {
name: $("#name").val(),
email: $("#email").val(),
message: $("#message").val()
}
$.ajax({
type: "POST",
url: "https://httpbin.org/post", // change it to your application specific url
data: my_inputs,
success: function(data, status){
console.log(data);
console.log(status);
$('.hover_bkgr_fricc').show();
},
error: function(err){
console.log(err);
}
});
// for resetting the form
$("#name").val("");
$("#email").val("");
$("#message").val("");
});

$('.popupCloseButton').click(function(){
$('.hover_bkgr_fricc').hide();
});
   /* Popup box BEGIN */
.hover_bkgr_fricc{
background:rgba(0,0,0,.4);
cursor:pointer;
display:none;
height:100%;
position:fixed;
text-align:center;
top:0;
width:100%;
z-index:10000;
}
.hover_bkgr_fricc .helper{
display:inline-block;
height:100%;
vertical-align:middle;
}
.hover_bkgr_fricc > div {
background-color: #fff;
box-shadow: 10px 10px 60px #555;
display: inline-block;
height: auto;
max-width: 551px;
min-height: 100px;
vertical-align: middle;
width: 60%;
position: relative;
border-radius: 8px;
padding: 15px 5%;
}
.popupCloseButton {
background-color: #fff;
border: 3px solid #999;
border-radius: 50px;
cursor: pointer;
display: inline-block;
font-family: arial;
font-weight: bold;
position: absolute;
top: -20px;
right: -20px;
font-size: 25px;
line-height: 30px;
width: 30px;
height: 30px;
text-align: center;
}
.popupCloseButton:hover {
background-color: #ccc;
}
.trigger_popup_fricc {
cursor: pointer;
font-size: 20px;
margin: 20px;
display: inline-block;
font-weight: bold;
}
/* Popup box BEGIN */
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="post">
<div class="fields">
<div class="field half">
<input type="text" name="name" id="name" placeholder="Name" />
</div>
<div class="field half">
<input type="email" name="email" id="email" placeholder="Email" />
</div>
<div class="field">
<textarea name="message" id="message" placeholder="Message"></textarea>
</div>
</div>
<ul class="actions">
<li><button class="trigger_popup_fricc primary" id="btn" type="submit" value="Send">Send</button></li>
</ul>
</form>
<div class="hover_bkgr_fricc">
<span class="helper"></span>
<div>
<div class="popupCloseButton">X</div>
<p>Add any HTML content<br />inside the popup box!</p>
</div>
</div>

关于javascript - HTML 表单提交弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57064395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com