doujing5726 2019-04-21 14:43 采纳率: 0%
浏览 2300

JavaScript 单击“提交”按钮后如何显示成功消息?

我有以下的javascript代码,以提交一个新的联系我们的表格。在按下提交按钮后,我希望html将显示一条成功消息。

知道我该怎么做吗?

$(function() {
  $('#contact-form').validator()

  $('#contact-form').on('submit', function(e) {
    // if the validator does not prevent form submit
    if (!e.isDefaultPrevented()) {
      const url = 'http://localhost/api/contact/'

      // POST values in the background the the script URL
      $.ajax({
        type: 'POST',
        url: url,
        data: $(this).serialize(),
        success: function(data) {
          // data = JSON object that contact.php returns

          // we recieve the type of the message: success x danger and apply it to the
          const messageAlert = 'alert-' + data.type
          const messageText = data.message

          // let's compose Bootstrap alert box HTML
          const alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>'

          // If we have messageAlert and messageText
          if (messageAlert && messageText) {
            // inject the alert to .messages div in our form
            $('#contact-form').find('.messages').html(alertBox)
            // empty the form
            $('#contact-form')[0].reset()
          }
        }
      })

      return false
    }
  })
})
  • 写回答

3条回答 默认 最新

  • duanmu0834 2019-04-21 14:56
    关注

    You should append your alertBox variable to an existing element in your HTML. For example, if your HTML element is has a div id of message:

    $("#message").append(alertBox);
    

    More info about append can be found here: http://api.jquery.com/append/

    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)