jquery实现不刷新页面的动态表单提交效果
本例未考虑其他,只完整演示无刷新提交表单,没有验证表单和阻止重复提交的问题,本站其他代码有以上的内容可以搜索查看
JavaScript Code
- $(function() {
- $('.error').hide();
- $('input.text-input').css({backgroundColor:"#FFFFFF"});
- $('input.text-input').focus(function(){
- $(this).css({backgroundColor:"#FFDDAA"});
- });
- $('input.text-input').blur(function(){
- $(this).css({backgroundColor:"#FFFFFF"});
- });
- /*Download by http://www.codefans.net*/
- $(".button").click(function() {
- // validate and process form
- // first hide any error messages
- $('.error').hide();
- var name = $("input#name").val();
- if (name == "") {
- $("label#name_error").show();
- $("input#name").focus();
- return false;
- }
- var email = $("input#email").val();
- if (email == "") {
- $("label#email_error").show();
- $("input#email").focus();
- return false;
- }
- var phone = $("input#phone").val();
- if (phone == "") {
- $("label#phone_error").show();
- $("input#phone").focus();
- return false;
- }
- 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;
- });
- });
- runOnLoad(function(){
- $("input#name").select().focus();
- });
XML/HTML Code
- <form name="contact" method="post" 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>
原文地址:http://www.freejs.net/article_biaodan_450.html