2012-04-03 61 views
0

用成功消息替換後,有沒有辦法在其div中返回聯繫表單?JQuery表單提交後的返回表單

表單看起來像這樣。

<div id="contact_form" > 
    <fieldset> 
     <form name="contact" method="post" action="" > 
      <p> 
       <label for="name">Name:</label> 
       <input name="name" id="name" value="" type="text" class="text-input" /> 
      </p> 
      <label class="error" for="name" id="name_error">*Required</label> 
      <p> 
       <label for="email">Email:</label> 
       <input name="email" id="email" value="" type="text" class="text-input" /> 
      </p> 
      <label class="error" for="email" id="email_error">*Required</label> 
      <p> 
       <label for="phone">Phone:</label> 
       <input name="phone" id="phone" value="" type="text" class="text-input" /> 
      </p> 
      <label class="error" for="phone" id="phone_error">*Required</label> 
      <p> 
       <label for="comment">Message:</label> 
       <textarea cols="20" rows="40" name="comment" id="comment" class="text-input"></textarea> 
      </p> 
      <label class="error" for="comment" id="comment_error">Please type a message.</label> 
      <p> 
       <input name="send" class="button" id="submit_btn" value="" type="submit" /> 
      </p> 
     </form> 
    </fieldset> 
    <div id="message"></div> 
</div> 

和表單提交是在這裏:

$(function() { 
    $('.error').hide(); 
    $('input.text-input').css({ 
     backgroundColor: "#FAFAFA" 
    }); 
    $('input.text-input').focus(function() { 
     $(this).css({ 
      backgroundColor: "#FFFFFF" 
     }); 
    }); 
    $('input.text-input').blur(function() { 
     $(this).css({ 
      backgroundColor: "#FAFAFA" 
     }); 
    }); 



    $(".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 comment = $("textarea#comment").val(); 
     if (comment == "") { 
      $("label#comment_error").show(); 
      $("textarea#comment").focus(); 
      return false; 
     } 

     var dataString = 'name=' + name + '&email=' + email + '&phone=' + phone + '&comment=' + comment; 
     //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>Email Sent!</h2>").append("<p> We will be in touch soon.</p>").hide().fadeIn(2000, function() { 
        $('#message').append("<img id='checkmark' src='img/check.png' />"); 
        setTimeout('$("#message").fadeOut("slow")', 2500); 
        $(document).ready(function() { 
         $('#contact_form').fadeOut(2000, function() { 
          var $newElement = $('<div id="contact_form">Form</div>'); 

          $(this).replaceWith($newElement); 
          $('contact_form').append($('<input name="name" id="name" value="" type="text" class="text-input" /></p>')); 
          $('contact_form').append($('<input name="name" id="name" value="" type="text" class="text-input" /></p>')); 
          $newElement.fadeIn(1000, function() { 
           document.location.reload(); 

           setTimeout(function() { 

            $('#name, #email, #phone, #comment').val('') 
           }, 1000); 


          }); 
         }); 
        }); 

       }); 
      } 
     }); 
     return false; 
    }); 
}); 


runOnLoad(function() { 

    $("input#name").select().focus(); 

    $('#contact_form input').focus(function() { 
     $(this).val(''); 
    }); 
    $('#contact_form textarea').focus(function() { 
     $(this).val(''); 
    }); 
}); 

我已經嘗試了幾件與replaceWith()和追加的,但對於現在的小格出現在重裝前貼在表格上。

任何想法?我覺得我快到了。在此先感謝


修飾克隆,表格字段保留其行爲聚焦狀態,但提交按鈕是不存在的單擊事件。當我點擊按鈕時,頁面被重新加載,而不是驗證表單域和/或發送數據到郵件程序。如何解決這個問題?提前致謝。

form_script.js:

$(function() { 


    $('.error').hide(); 
    $('input.text-input').css({backgroundColor:"#FAFAFA"}); 
    $('input.text-input').focus(function(){ 
    $(this).css({backgroundColor:"#FFFFFF"}); 
    }); 
    $('input.text-input').blur(function(){ 
    $(this).css({backgroundColor:"#FAFAFA"}); 
    }); 

    $('#contact_form input').focus(function() { 
    $(this).val(' '); 
}); 

$('#contact_form textarea').focus(function() { 
    $(this).val(''); 
}); 

//$('#contact_form').data('old-state', $('#contact_form').html()); 
var newForm = $('#contact_form').clone(true); 

    $(".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 comment = $("textarea#comment").val(); 
    if (comment == "") { 
    $("label#comment_error").show(); 
    $("textarea#comment").focus(); 
    return false; 
} 

    var dataString = '&name='+ name + '&email=' + email + '&phone=' + phone + '&comment=' + comment; 
    //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>Email Sent!</h2>") 
    .append("<p>We will be in touch soon.</p>") 
    .hide() 
    .fadeIn(2000, function() { 
     $('#message').append("<img id='checkmark' src='img/check.png' />"); 
     //setTimeout('$("#message").fadeOut("slow")',1000); 
     $('#message').append("<button id ='NewMail'></button>"); 

     $('#NewMail').click(function() { 
         // insert the new form 

      //$('#contact_form').html($('#contact_form').data('old-state')); 
      $('#contact_form').fadeIn(500).append(newForm); 

      $('#message').hide(); 
     }); 

    }); 


    } 
}); 

return false; 
}); 
}); 



runOnLoad(function(){ 


$("input#name").select().focus(); 


}); 

尋找在這裏,我看到了使用。數據()方法的建議,修改了我這樣的代碼和種得到同樣的效果。在問題是我有麻煩約束事件和屬性到原始形式的輸入:

$(function() { 


    $('.error').hide(); 
     $('input.text-input').css({backgroundColor:"#FAFAFA"}); 
    $('input.text-input').focus(function(){ 
     $(this).css({backgroundColor:"#FFFFFF"}); 
    }); 
$('input.text-input').blur(function(){ 
$(this).css({backgroundColor:"#FAFAFA"}); 
}); 

$('#contact_form input').focus(function() { 
    $(this).val(' '); 
}); 

$('#contact_form textarea').focus(function() { 
    $(this).val(''); 
}); 

$('#contact_form').data('old-state', $('#contact_form').html()); 


$(".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 comment = $("textarea#comment").val(); 
    if (comment == "") { 
    $("label#comment_error").show(); 
    $("textarea#comment").focus(); 
    return false; 
} 

    var dataString = '&name='+ name + '&email=' + email + '&phone=' + phone + '&comment=' + comment; 
    //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>Email Sent!</h2>") 
    .append("<p>We will be in touch soon.</p>") 
    .hide() 
    .fadeIn(2000, function() { 
     $('#message').append("<img id='checkmark' src='img/check.png' />"); 
     //setTimeout('$("#message").fadeOut("slow")',1000); 
     $('#message').append("<button id ='NewMail'></button>"); 

     $('#NewMail').click(function() { 
         // insert the new form 

      $('#contact_form').html($('#contact_form').data('old-state')); 

     }); 

    }); 


    } 
}); 

return false; 
}); 
}); 



runOnLoad(function(){ 


$("input#name").select().focus(); 


}); 

仍然讓我的頭繞jQuery魔法。有什麼建議麼。謝謝。

+1

更換改進形式能否請您解釋一下您的問題和目標一點點更清楚?這似乎有點模糊。 – 2012-04-03 18:20:34

+0

@ John Fisher,我有一個滑塊內的聯繫表單,所以有很多jquery飛行。滑動到聯繫頁面,表單div收集數據。 Onsubmit,表單淡出並顯示成功消息,然後淡出。所以我試圖在不重新加載頁面的情況下返回聯繫表單。聽起來像一座山,但這是主意。 – 2012-04-03 18:57:23

回答

0

而不是替換聯繫人表單,您可以插入後,然後刪除插入的元素。

你也可能想看看.clone()http://api.jquery.com/clone/,可以節省初始形式的克隆版本,並且比原來的

+0

這是別人的注意:-)否則我會去探索克隆並回報。謝謝。 – 2012-04-03 18:58:05