使用jQuery提交没有页面刷新的表单 [英] Submit A Form Without Page Refresh using jQuery

查看:106
本文介绍了使用jQuery提交没有页面刷新的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jQuery提交没有页面刷新的表单。我在网上找到了一些例子,比如这个:
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

I want to submit a form without page refresh using jQuery. I found some examples online, like this one: http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

但问题是他们有硬编码的表单字段:

but the problem is they have hard-coded form fields:

  <div id="contact_form">
  <form name="contact" action="">
    <fieldset>
      <label for="name" id="name_label">Name</label>
      <input type="text" name="name" id="name" size="30" value="" class="text-input" />
      <label class="error" for="name" id="name_error">This field is required.</label>

      <label for="email" id="email_label">Return Email</label>
      <input type="text" name="email" id="email" size="30" value="" class="text-input" />
      <label class="error" for="email" id="email_error">This field is required.</label>

      <label for="phone" id="phone_label">Return Phone</label>
      <input type="text" name="phone" id="phone" size="30" value="" class="text-input" />
      <label class="error" for="phone" id="phone_error">This field is required.</label>

        <br />
      <input type="submit" name="submit" class="button" id="submit_btn" value="Send" />
    </fieldset>
  </form>
  </div>

javascript:

And the javascript:

  var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone;
  //alert (dataString);return false;
  $.ajax({
    type: "POST",
    url: "bin/process.php",
    data: dataString,
    success: function() {
      $('#contact_form').html("<div id='message'></div>");
      $('#message').html("<h2>Contact Form Submitted!</h2>")
      .append("<p>We will be in touch soon.</p>")
      .hide()
      .fadeIn(1500, function() {
        $('#message').append("<img id='checkmark' src='images/check.png' />");
      });
    }
  });
  return false;

所以在上面的例子中, dataString 是从硬编码的表单字段创建的。我的表单是动态的,所以我不知道它的输入字段的名称。

So in the example above, the dataString is created from hard-coded form fields. My form is dynamic so I don't know the name of input fields it has.

注意:虽然表单字段是动态的,但是表单名称是硬编码的,所以我猜一个选项是通过子节点并解析值。但我想知道是否有更简单的方法。

Note: Although the form fields are dynamic, the form name is hard-coded, so I guess one option is to go through child nodes and parse the values. But I was wondering if there is an easier way.

推荐答案

数据:$(#contact_form form ).serializeArray()应该这样做。

这篇关于使用jQuery提交没有页面刷新的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆