<body> <div> <div> <span>ÎҵĸöÈË×ÊÁÏ</span> <span> <input type="button" value="ÐòÁл¯" /> </span> </div> <form action=""> <ul> <li>ÐÕÃû£º<input type="text" size="12" /></li> <li> <select> <option value="0">ÄÐ</option> <option value="1">Å®</option> </select> </li> <li><input type="checkbox" />×ÊÁÏÊÇ·ñ¿É¼û </li> <li></li> </ul> </form> </div> <script type="text/javascript"> $(function () { $("#btnAction").bind("click", function () { $("#litest").html($("form").serialize()); }) }) </script> </body>
----ʹÓÃajax()·½·¨¼ÓÔØ·þÎñÆ÷Êý¾Ý
ʹÓÃajax()·½·¨ÊÇ×îµ×²ã¡¢¹¦ÄÜ×îÇ¿´óµÄÇëÇó·þÎñÆ÷Êý¾ÝµÄ·½·¨£¬Ëü²»½ö¿ÉÒÔ»ñÈ¡·þÎñÆ÷·µ»ØµÄÊý¾Ý£¬»¹ÄÜÏò·þÎñÆ÷·¢ËÍÇëÇó²¢´«µÝÊýÖµ£¬ËüµÄµ÷ÓøñʽÈçÏ£º
jQuery.ajax([settings])»ò$.ajax([settings])
ÆäÖвÎÊýsettingsΪ·¢ËÍajaxÇëÇóʱµÄÅäÖöÔÏó£¬ÔڸöÔÏóÖУ¬url±íʾ·þÎñÆ÷ÇëÇóµÄ·¾¶£¬dataΪÇëÇóʱ´«µÝµÄÊý¾Ý£¬dataTypeΪ·þÎñÆ÷·µ»ØµÄÊý¾ÝÀàÐÍ£¬successΪÇëÇó³É¹¦µÄÖ´ÐеĻص÷º¯Êý£¬typeΪ·¢ËÍÊý¾ÝÇëÇóµÄ·½Ê½£¬Ä¬ÈÏΪget¡£
<body> <div> <div> <span>¼ì²âÊý×ÖµÄÆæżÐÔ</span> <span> <input type="button" value="¼ì²â" /> </span> </div> <ul> <li>ÇëÇóÊäÈëÒ»¸öÊý×Ö <input type="text" size="12" /> </li> </ul> </div> <script type="text/javascript"> $(function () { $("#btnCheck").bind("click", function () { $.ajax({ url:"http://www.imooc.com/data/check.php", data: { num: $("#txtNumber").val() }, type:"post", success: function (data) { $("ul").append("<li>ÄãÊäÈëµÄ<b> " + $("#txtNumber").val() + " </b>ÊÇ<b> " + data + " </b></li>"); } }); }) }); </script> </body>
----ʹÓÃajaxSetup()·½·¨ÉèÖÃÈ«¾ÖAjaxĬÈÏÑ¡Ïî
ʹÓÃajaxSetup()·½·¨¿ÉÒÔÉèÖÃAjaxÇëÇóµÄһЩȫ¾ÖÐÔÑ¡ÏîÖµ£¬ÉèÖÃÍê³Éºó£¬ºóÃæµÄAjaxÇëÇ󽫲»ÐèÒªÔÙÌí¼ÓÕâЩѡÏîÖµ£¬ËüµÄµ÷ÓøñʽΪ£º
jQuery.ajaxSetup([options])»ò$.ajaxSetup([options])
¿ÉÑ¡Ïîoptions²ÎÊýΪһ¸ö¶ÔÏó£¬Í¨¹ý¸Ã¶ÔÏóÉèÖÃAjaxÇëÇóʱµÄÈ«¾ÖÑ¡ÏîÖµ¡£
<body> <div> <div> <span>ÆæżÐÔºÍÊÇ·ñ´óÓÚ0</span> <span> <input type="button" value="ÑéÖ¤1" /> <input type="button" value="ÑéÖ¤2" /> </span> </div> <ul> <li>ÇëÇóÊäÈëÒ»¸öÊý×Ö <input type="text" size="12" /> </li> </ul> </div> <script type="text/javascript"> $(function () { $.ajaxSetup({ url:"http://www.imooc.com/data/check.php", data: { num: $("#txtNumber").val() }, type:"post", success:function(data){$("ul").append("<li>ÄãÊäÈëµÄ<b> " + $("#txtNumber").val() + " </b>ÊÇ<b> " + data + " </b></li>"); } }); $("#btnShow_1").bind("click", function () { $.ajax({ data: { num: $("#txtNumber").val() }, url: "http://www.imooc.com/data/check.php" }); }) $("#btnShow_2").bind("click", function () { $.ajax({ data: { num: $("#txtNumber").val() }, url: "http://www.imooc.com/data/check_f.php" }); }) }); </script> </body>
----ʹÓÃajaxStart()ºÍajaxStop()·½·¨
ajaxStart()ºÍajaxStop()·½·¨ÊÇ°ó¶¨Ajaxʼþ¡£ajaxStart()·½·¨ÓÃÓÚÔÚAjaxÇëÇó·¢³öÇ°´¥·¢º¯Êý£¬ajaxStop()·½·¨ÓÃÓÚÔÚAjaxÇëÇóÍê³Éºó´¥·¢º¯Êý¡£ËüÃǵĵ÷ÓøñʽΪ£º
$(selector).ajaxStart(function())ºÍ$(selector).ajaxStop(function())
ÆäÖУ¬Á½¸ö·½·¨ÖÐÀ¨ºÅ¶¼Êǰ󶨵ĺ¯Êý£¬µ±·¢ËÍAjaxÇëÇóÇ°Ö´ÐÐajaxStart()·½·¨°ó¶¨µÄº¯Êý£¬ÇëÇó³É¹¦ºó£¬Ö´ÐÐajaxStop ()·½·¨°ó¶¨µÄº¯Êý¡£
<body> <div> <div> <span>¼ÓÔØÒ»¶ÎÎÄ×Ö</span> <span> <input type="button" value="¼ÓÔØ" /> </span> </div> <ul> <li></li> </ul> </div> <script type="text/javascript"> $(function () { $("#divtest").ajaxStart(function(){ $(this).html("ÕýÔÚÇëÇóÊý¾Ý..."); }); $("#divtest").ajaxStop(function(){ $(this).html("Êý¾ÝÇëÇóÍê³É£¡"); }); $("#btnShow").bind("click", function () { var $this = $(this); $.ajax({ url: "http://www.imooc.com/data/info_f.php", dataType: "json", success: function (data) { $this.attr("disabled", "true"); $("ul").append("<li>ÎÒµÄÃû×ֽУº" + data.name + "</li>"); $("ul").append("<li>ÄÐÅóÓѶÔÎÒ˵£º" + data.say + "</li>"); } }); }) }); </script> </body>
-----jQuery³£Óòå¼þ-----
----±íµ¥ÑéÖ¤²å¼þ¡ª¡ªvalidate
¸Ã²å¼þ×Ô´ø°üº¬±ØÌî¡¢Êý×Ö¡¢URLÔÚÄÚÈݵÄÑéÖ¤¹æÔò£¬¼´Ê±ÏÔʾÒì³£ÐÅÏ¢£¬´ËÍ⣬»¹ÔÊÐí×Ô¶¨ÒåÑéÖ¤¹æÔò£¬²å¼þµ÷Ó÷½·¨ÈçÏ£º
$(form).validate({options})
ÆäÖÐform²ÎÊý±íʾ±íµ¥ÔªËØÃû³Æ£¬options²ÎÊý±íʾµ÷Ó÷½·¨Ê±µÄÅäÖöÔÏó£¬ËùÓеÄÑéÖ¤¹æÔòºÍÒì³£ÐÅÏ¢ÏÔʾµÄλÖö¼ÔڸöÔÏóÖнøÐÐÉèÖá£
<body> <form method="get" action="#"> <div> <div> <span>±íµ¥ÑéÖ¤²å¼þ</span> <span> <input type="submit" value="Ìá½»" /> </span> </div> <div> <span>ÓÊÏ䣺</span><br /> <input type="text" /><br /> <span></span> </div> </div> </form> <script type="text/javascript"> $(function () { $("#frmV").validate( { /*×Ô¶¨ÒåÑéÖ¤¹æÔò*/ rules: { email:{ required:true, email:true } } }, /*´íÎóÌáʾλÖÃ*/ errorPlacement: function (error, element) { error.appendTo(".tip"); } } ); }); </script> </body> </html>
¡¡