jquery submit()不会做任何事情

时间:2011-11-23 20:18:09

标签: jquery

有jquery submit()的问题。问题是它在网站上不起作用,它将我发送到动作网址(如使用get),而不是阻止它并在submit()中运行该功能。如果在控制台中添加submit() - 脚本,它可以工作。怎么会?

这是我的js文件:

$(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);
      });
  });
});

这是我的表格:

$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提交粘贴到控制台而不是页面加载时,jquery如何提交只会做出反应?我没有得到任何javascript错误,并尝试了很多不同的jquery版本。

2 个答案:

答案 0 :(得分:2)

问题是该事件未触发。你说,代码在你手动触发事件后执行得很好。由于某些原因,事件无法触发......第一种情况通常是因为事件在呈现html之前已连线。您可以通过使用委托来解决这个问题,但我相信(在“解决”问题之后)您的事件未触发,因为您正在接线的事件位于form submissionyou do not have a submit button on your form。您可能想尝试将按钮设置为提交按钮,或者在按钮单击时触发代码而不是表单提交。

以下是使用delegates

的代码
$("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中比旧版本更有效):

$("body").on("submit", "#contact-form", function() {
  //code here
});

答案 1 :(得分:1)

我认为你的按钮可能需要一个type =“submit”才能工作。 这就是jquery docs所说的

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

相关问题