表单提交停止刷新


Form Submit stop refresh

好吧,这是一个使用ajax导航加载的页面,页面加载得很好。我正在尝试制作它,这样当页面提交时,它就不会刷新整个页面。我四处寻找了一段时间,仍然找不到解决办法。

<head>
  <script>
    $(document).on('submit', '#changePassword', function() {
      $("#content").load("core.home");
      return false;
    });
  </script>
</head>
<?php if( !preg_match( "/index.php/i", $_SERVER[ 'PHP_SELF'] ) ) { die(); } ?>
<form action="" method="post" id="changePassword">
  <div class="box">
    <div class="boxhead purple purple">
      <strong>Change password</strong>
    </div>
    <?php if( $_POST[ 'submit'] ) { try { $oldpassword=$ core->clean( $_POST['current_password'] ); $oldpassword_enc = $core->encrypt( $oldpassword ); $newpassword = $core->clean( $_POST['new_password'] ); $newpassword_enc = $core->encrypt( $newpassword ); if( !$oldpassword or !$newpassword) { throw new Exception(
    "All fields are required." ); } elseif( $oldpassword_enc != $user->data['password'] ) { throw new Exception( "The password you entered does not match the one we have on record." ); } else { $db->query( "UPDATE users SET password = '{$newpassword_enc}'
    WHERE id = '{$user->data['id']}'" ); echo "
    <div class=' "square good'">"; echo "<strong>Success</strong>"; echo "
      <br />"; echo "Password successfully changed!"; echo "</div>"; $db->query( "INSERT INTO logs VALUES (NULL, 'Changed Password <font color=' "51c833'"><b>Success</b></font>', NULL, '<b>{$user->data['fullUsername']}</b> ({$_SERVER['REMOTE_ADDR']})', '3') "
    ); } } catch( Exception $e ) { echo "
    <div class=' "square bad'">"; echo "<strong>Error</strong>"; echo "
      <br />"; echo $e->getMessage(); echo "</div>"; $db->query( "INSERT INTO logs VALUES (NULL, 'Changed Password <font color=' "FF0000'"><b>Fail</b></font> Reason: {$e->getMessage()}', NULL, '<b>{$user->data['fullUsername']}</b> ({$_SERVER['REMOTE_ADDR']})',
    '3') " ); } } ?>
    <table width="100%" cellpadding="3" cellspacing="0">
      <?php echo $core->buildField( "password", "required", "current_password", "Current password" ); echo $core->buildField( "password", "required", "new_password", "New password"); ?>
    </table>
  </div>
  <div class="box" align="right">
    <button class="button" type="submit" name="submit" value="Submit" id="button">Submit</button>
  </div>
</form>
<?php echo $core->buildFormJS('changePassword'); ?>

您需要使用e.preventDefault来停止提交按钮的默认行为(即在提交时重新加载)

$(document).on('submit', '#changePassword', function(e)
 {  
  e.preventDefault();
 });