HTML表单提交弹出窗口

时间:2019-07-16 19:41:17

标签: javascript jquery html css forms

我的网站上有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();
    });
});

但是它不起作用。没有弹出窗口。任何有关如何完成我要完成的任务的指导都将不胜感激。

2 个答案:

答案 0 :(得分:1)

问题是您在发送表单并离开页面后触发了click事件。如果您想停留在该页面上,建议您使用AJAX发送表单并处理回复。

在这里您可以使用.post()找到有关AJAX的提示。首先,我打电话给event.preventDefault()以防止所单击按钮的所有默认行为。然后读取所有表单数据,并将其作为AJAX调用的输入。

如果AJAX成功,我将显示弹出窗口,否则将跳至.fail()

这只是一个提示。您仍然需要编写服务器端脚本来处理表单数据并返回响应,并在JS中编写成功和失败案例。

$(window).on("load", function () {
    $(".trigger_popup_fricc").click(function(event){
        event.preventDefault();
        var inputData = {
            name: $("#name").val(),
            email: $("email").val(),
            message: $("#message").val()
        }
        $.post('https://mywebsite.com/formdata', inputData, function(data){
            // Process the response with data returned from the script
            $('.hover_bkgr_fricc').show();
        })
        .fail(function() {
            // Process if error
        });
    });
    $('.hover_bkgr_fricc').click(function(){
        $('.hover_bkgr_fricc').hide();
    });
    $('.popupCloseButton').click(function(){
        $('.hover_bkgr_fricc').hide();
    });
});

我还对您的HTML进行了一些修改。

<form>
<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 primary" type="submit" value="Send" /></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>

答案 1 :(得分:1)

您只需要向enpoint URL添加一个ajax请求并获取响应。但是,正如另一个答案所述,您需要使用from woocommerce import API wcapi = API( url = 'foo.bar.com', consumer_key = 'yourKeyHere', consumer_secret = 'yourSecretHere', version = 'wc/3' ) def checkLicence(key: str, product: str, version: str, email: str) -> bool: endpoint = "?wc-api=software-api" first = "&request=check&email=%s" % (email) second = "&licence_key=%s" % (key) third = "&product_id=%s&version=%s" % (product, version) r = wcapi.get(endpoint + first + second + third) if 200 == r.status_code: success = r.json()['success'] return success else: return False 来避免在单击发送按钮时刷新。

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

preventDefault()
$(".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 */