【发布时间】:2012-04-04 13:41:31
【问题描述】:
这是一个关于如何使用 Jquery 表单插件提交表单和使用 html 格式检索数据的简单示例 html代码
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
// prepare the form when the DOM is ready
$(document).ready(function() {
// bind form using ajaxForm
$('#htmlForm').ajaxForm({
// target identifies the element(s) to update with the server response
target: '#htmlExampleTarget',
// success identifies the function to invoke when the server response
// has been received; here we apply a fade-in effect to the new content
success: function() {
$('#htmlExampleTarget').fadeIn('slow');
}
});
});
</script>
</head>
<body>
<form id="htmlForm" action="post.php" method="post">
Message: <input type="text" name="message" value="Hello HTML" />
<input type="submit" value="Echo as HTML" />
</form>
<div id="htmlExampleTarget"></div>
</body>
</html>
PHP 代码
<?php
echo '<div style="background-color:#ffa; padding:20px">' . $_POST['message'] . '</div>';
?>
这很好用 如果我需要序列化表单字段,我需要知道什么,那么如何通过 JS 函数传递这个选项 我也想在处理表单时显示加载消息 我也应该怎么做 谢谢你
【问题讨论】:
-
为了加载消息,您可以通过这种方式使用 beforeSend 函数: beforeSend: function(){ $("#htmlExampleTarget").html('Loading...'); },