首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jquery submit()不会做任何事情

jquery submit()不会做任何事情
EN

Stack Overflow用户
提问于 2011-11-24 04:18:09
回答 2查看 262关注 0票数 0

在使用jquery submit()时遇到问题。问题是它在站点上不起作用,它会将我发送到action url (作为im使用get),而不是阻止它并在submit()中运行函数。如果在控制台中添加submit()-script就可以了。怎么会这样?

这是我的js文件:

代码语言:javascript
复制
$(function(ready){
  $('#contact-form button')
    .removeClass('disabled')
    .removeAttr('disabled');

  $('#contact-form').submit(function(e){
    alert('????????????????????????????');
    return false;
      e.preventDefault();

      $this = $(this);
      $this.addClass('disabled').attr('disabled', true);
      $('#contact-form img').addClass('showLoader');

      $.post('theme/widgets/contact-form/ajax/send-mail.php', $('#contact-form').serialize(), function(d){
        $this.removeClass('disabled').removeAttr('disabled');
        $('#contact-form img').removeClass('showLoader');

        try {
          $msg = $.parseJSON(d);
        } catch (e) {
          console.log(e, d);
          $msg.error = true;
          $msg.message = "Unknown error occoured. See console for more information.";
        }

        if (!$msg.error) {
          $('#contact-form input').val('');
          $('#contact-form textarea').val('');
        }

        alert($msg.message);
      });
  });
});

这是我的表格:

代码语言:javascript
复制
$html = "<form id='contact-form'>
  <label>".__('name', false, $ld)."</label>
  <input type='text' name='name' /><br />
  <label>".__('email', false, $ld)."</label>
  <input type='text' name='email' /><br />
  <label>".__('body', false, $ld)."</label>
  <textarea name='body'></textarea><br />
  <img src='".THEME."images/ajax-loader-small.gif' />
  <button class='disabled' disabled='disabled'><span>".__('send', false, $ld)."</span></button>
</form>";

为什么jquery提交只在我粘贴到控制台时才反应,而不是在页面加载时反应呢?我没有收到任何javascript错误,我已经尝试了很多不同的jquery版本。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-24 04:24:47

问题是事件没有触发。代码,你说,执行的很好...在手动触发事件之后。事件不能被触发有几个原因...第一种情况通常是因为事件在html被呈现之前就被连接起来了。你可以通过使用委托来解决这个问题,但是我相信(在‘解决’这个问题之后)你的事件不会被触发,因为你连接的事件是在form submission上,而是在you do not have a submit button on your form上。您可能希望尝试使按钮成为提交按钮,或者使代码在单击按钮时触发,而不是在表单提交时触发。

下面是使用delegates的代码

代码语言:javascript
复制
$("body").delegate("#contact-form", "submit", function() {
    alert('????????????????????????????');
    return false;
      e.preventDefault();

      $this = $(this);
      $this.addClass('disabled').attr('disabled', true);
      $('#contact-form img').addClass('showLoader');

      $.post('theme/widgets/contact-form/ajax/send-mail.php', $('#contact-form').serialize(), function(d){
        $this.removeClass('disabled').removeAttr('disabled');
        $('#contact-form img').removeClass('showLoader');

        try {
          $msg = $.parseJSON(d);
        } catch (e) {
          console.log(e, d);
          $msg.error = true;
          $msg.message = "Unknown error occoured. See console for more information.";
        }

        if (!$msg.error) {
          $('#contact-form input').val('');
          $('#contact-form textarea').val('');
        }

        alert($msg.message);
      });
});

jQuery 1.7调用(推荐,委派在1.7中比旧版本更有效):

代码语言:javascript
复制
$("body").on("submit", "#contact-form", function() {
  //code here
});
票数 2
EN

Stack Overflow用户

发布于 2011-11-24 04:37:17

我认为你的按钮可能需要一个type=“提交”才能工作。不管怎样,jquery文档就是这么说的

http://api.jquery.com/submit/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8248522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档