例子1
HTML文件:
代码如下 | 复制代码 |
<!DOCTYPE html> <html> <head> <script src="/jquery-latest.js"></script> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <form action="" id="searchForm"> 请输入一个数字,测试<input type="number" name="s" placeholder="Enter a number..." /> + <input type="number" name="b" placeholder="Enter a number..." /> <input type="button" value="POST" id="su" /> </form> <!-- the result of the search will be rendered inside this div --> <div id="result"></div> <script> $(function(){ $("#su").click(function() { //var form = $('#searchForm').serialize(); var s =$("input:[name=s]").val(); var b =$("input:[name=b]").val(); $.post("test.php",{s:s,b:b},function(data){ $("#result").html(data) }); // alert("fsdf"); }); }); </script> </body> </html> php文件: <?php |
例子2
本例是用jquery.form.js form提交,action传递的
加了一个判断填写的项目是否填写
直接替换本页面ID
使用jquery.form.js
主要代码
代码如下 | 复制代码 |
<script src="../jquery.js"></script> <script src="jquery.form.js"></script> <script> // prepare the form when the DOM is ready $(document).ready(function() { // bind form using ajaxForm $('#htmlForm').ajaxForm({ beforeSubmit:checkForm,//提交前运行函数验证 // target identifies the element(s) to update with the server response target: '#htmlExampleTarget', // success identifies the function to invoke when the server response } }); </script> <form id="htmlForm" action="ok.php" method="get" name="htmlForm"> |