jquery ajax可以只告诉表单id吗

发布于 2024-12-08 06:41:18 字数 382 浏览 0 评论 0原文

当我使用 jquery 发送 ajax 请求时,我指定一个 data 数组,就像

data: {param1: 'somevalue', param2: 'another value' }

我看到一些用 MooTools 1.2 编写的旧代码一样,MooTools 似乎可以按原样发送整个表单,而不需要一一指定字段。您只需为其提供表单 ID,在本例中为 theform

var theNewRequest = new Request.HTML({url:'ajaxpage.php'}).post($('theform'));

jquery中有类似的东西吗?

When I send ajax requests with jquery, I specify a data array, like

data: {param1: 'somevalue', param2: 'another value' }

I saw some old code written in MooTools 1.2 and it seems MooTools makes it possible to send the whole form as is, without the need to specify the fields one by one. You just give it the form id, in this case theform.

var theNewRequest = new Request.HTML({url:'ajaxpage.php'}).post($('theform'));

Is there something similar in jquery?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆梦 2024-12-15 06:41:18

是的,您可以使用序列化函数来发送表单的序列化内容。

$.post("ajaxpage.php", $("#theform").serialize());

Yes, you can use the serialize function to send the serialized contents of the form.

$.post("ajaxpage.php", $("#theform").serialize());
动听の歌 2024-12-15 06:41:18

$.post('somewhere',$('formid').serialize(), function(data){}) 怎么样?

How about $.post('somewhere',$('formid').serialize(), function(data){})?

累赘 2024-12-15 06:41:18

我几周前编写了这个脚本来简化 AJAX 表单提交:

/**
 * 
 * Autesion King of Thebes
 * 
 * Also, a simple script by zzzzBov to automatically submit a form via AJAX
 */
(function($){
"use strict";
function autesion(options){
  var settings;
  settings = $.extend(true, {}, autesion.defaultSettings, options);
  this.submit(function(e){
    var $this, ajaxSettings, $form;

    if (!e.isDefaultPrevented())
    {
      $this = $(this);
      $form = $(e.target);
      ajaxSettings = $.extend({}, settings.ajaxSettings, {
        'data':$form.serialize(),
        'type':$form.attr('method') || 'GET',
        'url':$form.attr('action')
      });

      if (settings.useEvents)
      {
        ajaxSettings.beforeSend=function(j,s){
          $this.trigger(new $.Event('autesion.beforeSend', {jqXHR:j, settings:s}));
        };
        ajaxSettings.complete=function(j,t){
          $this.trigger(new $.Event('autesion.complete', {jqXHR:j, textStatus:t}));
        };
        ajaxSettings.error=function(j,t,e){
          $this.trigger(new $.Event('autesion.error', {jqXHR:j, textStatus:t, errorThrown:e}));
        };
        ajaxSettings.success=function(d,t,j){
          $this.trigger(new $.Event('autesion.success', {jqXHR:j, textStatus:t, ajaxData:d}));
        };
      }

      e.preventDefault();
      $.ajax(ajaxSettings);
    }
  });

  if (settings.useEvents)
  {
    this.bind('autesion.beforeSend', settings.beforeSend)
      .bind('autesion.complete', settings.complete)
      .bind('autesion.error', settings.error)
      .bind('autesion.success', settings.success);
  }

  return this;
};
autesion.defaultSettings={
  'ajaxSettings':{},
  'beforeSend':$.noop,
  'complete':$.noop,
  'error':$.noop,
  'success':$.noop,
  'useEvents':true
};

$.fn.autesion = autesion;

}(jQuery));

我确信它可以得到显着改进,但只要您不需要文件输入支持,它就应该按原样工作。

要使用它,只需调用:

$('form').autesion();

缩写形式为:

$form = $('form');
$form.ajax({
  'data': $form.serialize(),
  'type': $form.attr('method') || 'GET',
  'url': $form.attr('action')
});

I wrote this script a couple weeks back to simplify AJAX form submission:

/**
 * 
 * Autesion King of Thebes
 * 
 * Also, a simple script by zzzzBov to automatically submit a form via AJAX
 */
(function($){
"use strict";
function autesion(options){
  var settings;
  settings = $.extend(true, {}, autesion.defaultSettings, options);
  this.submit(function(e){
    var $this, ajaxSettings, $form;

    if (!e.isDefaultPrevented())
    {
      $this = $(this);
      $form = $(e.target);
      ajaxSettings = $.extend({}, settings.ajaxSettings, {
        'data':$form.serialize(),
        'type':$form.attr('method') || 'GET',
        'url':$form.attr('action')
      });

      if (settings.useEvents)
      {
        ajaxSettings.beforeSend=function(j,s){
          $this.trigger(new $.Event('autesion.beforeSend', {jqXHR:j, settings:s}));
        };
        ajaxSettings.complete=function(j,t){
          $this.trigger(new $.Event('autesion.complete', {jqXHR:j, textStatus:t}));
        };
        ajaxSettings.error=function(j,t,e){
          $this.trigger(new $.Event('autesion.error', {jqXHR:j, textStatus:t, errorThrown:e}));
        };
        ajaxSettings.success=function(d,t,j){
          $this.trigger(new $.Event('autesion.success', {jqXHR:j, textStatus:t, ajaxData:d}));
        };
      }

      e.preventDefault();
      $.ajax(ajaxSettings);
    }
  });

  if (settings.useEvents)
  {
    this.bind('autesion.beforeSend', settings.beforeSend)
      .bind('autesion.complete', settings.complete)
      .bind('autesion.error', settings.error)
      .bind('autesion.success', settings.success);
  }

  return this;
};
autesion.defaultSettings={
  'ajaxSettings':{},
  'beforeSend':$.noop,
  'complete':$.noop,
  'error':$.noop,
  'success':$.noop,
  'useEvents':true
};

$.fn.autesion = autesion;

}(jQuery));

I'm certain it can be significantly improved, but it should work as-is as long as you don't require file input support.

To use it, simply call:

$('form').autesion();

The short form is:

$form = $('form');
$form.ajax({
  'data': $form.serialize(),
  'type': $form.attr('method') || 'GET',
  'url': $form.attr('action')
});
忘羡 2024-12-15 06:41:18

jQuery .serialize() 方法以标准 URL 编码表示法创建文本字符串。它对表示一组表单元素的 jQuery 对象进行操作。

http://api.jquery.com/serialize/

jQuery .serialize() method creates a text string in standard URL-encoded notation. It operates on a jQuery object representing a set of form elements.

http://api.jquery.com/serialize/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文