JQuery表单提交问题?

时间:2010-03-23 01:04:02

标签: jquery html

我对JQuery不熟悉,并且想知道当用户提交更改<p>Your changes have been saved.</p>时,如何显示以下文本?如何修复我的代码以显示此消息?

JQuery脚本。

$(function() {

$(".save-button").click(function() {
    $.post($("#contact-form").attr("action"), $("#contact-form").serialize(), function(html) {
        $("div.contact-info-form").html(html);
    });
    return false; // prevent normal submit
});

});

这是html。

<div id="contact-info-form" class="form-content">

<h2>Contact Information</h2>
<form method="post" action="index.php" id="contact-form">
<fieldset>
<ul>
<li><label for="address">Address 1: </label><input type="text" name="address" id="address" size="25" class="input-size" value="<?php if (isset($_POST['address'])) { echo $_POST['address']; } else if(!empty($address)) { echo $address; } ?>" /></li>
<li><label for="address_two">Address 2: </label><input type="text" name="address_two" id="address_two" size="25" class="input-size" value="<?php if (isset($_POST['address_two'])) { echo $_POST['address_two']; } else if(!empty($address_two)) { echo $address_two; } ?>" /></li>
<li><label for="city_town">City/Town: </label><input type="text" name="city_town" id="city_town" size="25" class="input-size" value="<?php if (isset($_POST['city_town'])) { echo $_POST['city_town']; } else if(!empty($city_town)) { echo $city_town; } ?>" /></li>
<li><label for="state_province">State/Province: </label>
<?php

echo '<select name="state_province" id="state_province">' . "\n";
  foreach($state_options as $option) {
    if ($option == $state_province) {
      echo '<option value="' . $option . '" selected="selected">' . $option . '</option>' . "\n";
    } else {
      echo '<option value="'. $option . '">' . $option . '</option>'."\n";
    }
  }
echo '</select>';

?>
</li>

<li><label for="zipcode">Zip/Post Code: </label><input type="text" name="zipcode" id="zipcode" size="5" class="input-size" value="<?php if (isset($_POST['zipcode'])) { echo $_POST['zipcode']; } else if(!empty($zipcode)) { echo $zipcode; } ?>" /></li>

<li><label for="country">Country: </label>
<?php

echo '<select name="country" id="country">' . "\n";
  foreach($countries as $option) {
    if ($option == $country) {
      echo '<option value="' . $option . '" selected="selected">' . $option . '</option>' . "\n";
    } 
    else if($option == "-------------") {
      echo '<option value="' . $option . '" disabled="disabled">' . $option . '</option>';
    }
    else {
      echo '<option value="'. $option . '">' . $option . '</option>'."\n";
    }
  }
echo '</select>';

?>
</li>

<li><label for="email">Email Address: </label><input type="text" name="email" id="email" size="25" class="input-size" value="<?php if (isset($_POST['email'])) { echo $_POST['email']; } else if(!empty($email)) { echo $email; } ?>" /><br /><span>We don't spam or share your email with third parties. We respect your privacy.</span></li>

<li><p>Changes have been saved</p><input type="submit" name="submit" value="Save Changes" class="save-button" />
    <input type="hidden" name="contact_info_submitted" value="true" />
<input type="submit" name="submit" value="Preview Changes" class="preview-changes-button" /></li>
</ul>
</fieldset>

</form>

</div>

3 个答案:

答案 0 :(得分:2)

回调函数中的

 $.post($("#contact-form").attr("action"), $("#contact-form").serialize(), function(html) {
    $("div.contact-info-form").html(html);
    $('.yourConfirmationDiv').html('<p>Changes saved!</p>');
});

答案 1 :(得分:2)

您可以使用$.ajax来处理更多错误和超时等事件。

$.ajax({
    url: $("#contact-form").attr("action"),
    type: "POST",
    data: $("#contact-form").serialize(),
    success: function(html){
       $("div.contact-info-form").html(html);
       // Add saved message to DOM
    }
    error: function(error){
       // Handle error
    }
});

答案 2 :(得分:0)

如果要将其显示在表单上方,请执行以下操作:

$(function() {
  $(".save-button").click(function() {
     $.post($("#contact-form").attr("action"), $("#contact-form").serialize(),
       function(html) {
         $("div.contact-info-form")
               .html("<p>Your changes have been saved.</p>" + html);
      });
      return false; // prevent normal submit
  });
});