【问题标题】:Form submission js/jquery staying on same page表单提交 js/jquery 停留在同一页面
【发布时间】:2016-11-04 15:52:41
【问题描述】:

我正在尝试在 CMS 页面中提交表单,并且不希望在处理 php 邮件脚本后加载新页面。相反,我需要在不重新加载页面或转到其他页面的情况下显示单行成功消息(而不是警报弹出框)。

这是我拥有的代码,我的理解是 event.preventDefault 应该允许留在同一页面上,$("#contactResponse").html(data); 应该将成功消息放在同一页面上。

这是我应该接收成功消息的表单上方的 div 标签(我也尝试将它放在我的表单之后):

<div id="contactResponse"></div>

这是我的表单标签:

编辑:也包括我的表单代码:(div 类的东西来自其他人所做的自定义 css)

<form id="contactForm" name="contactForm" method="post" action="/myemail.php">

<div class="form-group"><input class="form-control" name="email" type="email" placeholder="Email Address" /></div>
</div>
<div class="col-sm-4">
<div class="form-group"><input class="form-control" name="question" type="text" placeholder="What is your question?" /></div>
</div>
<div class="form-group"><input class="btn btn-warning btn-block" type="submit" value="Request Information"></div>
</form>

这是我的表单和 div 标签上方的脚本:

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
    $("#contactForm").submit(function(event) {
        event.preventDefault();

        var $form = $(this),
             $submit = $form.find('button[type="submit"]'),
             email_value = $form.find('input[name="email"]').val(),
             message_value = $form.find('input[name="question"]').val(),
             url = $form.attr('action');

        var posting = $.post(url, { 
            email: email_value, 
            question: message_value 
        });

        posting.done(function(data) {
            $("#contactResponse").html(data);
        });
    });
</script>

电子邮件有效,但 php 脚本在服务器上,它会将我带到另一个页面。

谁能给我一些建议/建议。

谢谢

【问题讨论】:

  • 请发布完整的 HTML 表单。您是否在浏览器的控制台中遇到任何错误?
  • 如果 JS 位于 HTML 主体之上,那么您需要将 jQuery 代码包装在 document.ready 处理程序中:$(function() { /* your code here... */ });。目前您的表单正在以标准方式提交,不涉及 AJAX
  • 谢谢j08691,我已经添加了表单代码谢谢Rory,你能告诉我如何包装吗,抱歉我不知道
  • @Rory:我尝试将 JS 放在底部,即在我的表单和部分标签之后,但它仍然相同:(
  • 所以从其他一些建议中,我也尝试过包含在这样的函数中: $(document).ready(function() { OR $(function(){ 我在带有“ }) ”的脚本标记但是没有运气... :( 下面是开始的样子:

标签: javascript jquery


【解决方案1】:

试试这个,它对我有用

<script>
  // Get the form.
  var form = $('#contactForm');
  // Get the messages div.
  var formMessages = $('#contactResponse');
  // Set up an event listener for the contact form.
  $(form).submit(function(event) {
    // Stop the browser from submitting the form.
    event.preventDefault();
    // Serialize the form data.
    var formData = $(form).serialize();
    // Submit the form using AJAX.
    $.ajax({
        type: 'POST',
        url: $(form).attr('action'),
        data: formData
    }).done(function(response) {
        // Make sure that the formMessages div has the 'success' class.
        $(formMessages).removeClass('error');
        $(formMessages).addClass('success');
        // Set the message text.
        $(formMessages).text(response);
        // Clear the form.
        $('#name').val('');
        $('#email').val('');
        // $('#subject').val('');
        // $('#company').val('');
        $('#message').val('');
        $(form).hide();
    }).fail(function(data) {
        // Make sure that the formMessages div has the 'error' class.
        $(formMessages).removeClass('success');
        $(formMessages).addClass('error');
        $( "#contact" ).children(".loading").remove();
        // Set the message text.
        if (data.responseText !== '') {
            $(formMessages).text(data.responseText);
        } else {
            $(formMessages).text('Oops! An error occured and your message could not be sent.');
        }
    });
</script>

【讨论】:

  • 感谢 ashraf,我会尝试理解它,看看它是否按预期工作
  • ashraf,我应该把上面的代码放在一个 SCRIPT 标签中还是需要其他一些封闭?
  • 是的,它应该包含在脚本标签中(我编辑了我的答案),这个 js 代码基本上应该替换你的表单提交处理程序。
猜你喜欢
  • 2023-03-15
  • 2013-06-24
  • 2015-01-02
  • 1970-01-01
  • 2017-01-11
  • 1970-01-01
  • 2011-08-09
  • 2013-06-21
  • 1970-01-01
相关资源
最近更新 更多