»¶Ó½øÈëJavaÉçÇøÂÛ̳£¬Óë200Íò¼¼ÊõÈËÔ±»¥¶¯½»Á÷ >>½øÈë
Ajax ±íµ¥Ìá½»ÊÇÒ»¸ö¹¦ÄÜÇ¿´óµÄ¼¼Êõ£¬ÌṩһÖÖ·¢ËÍ web ±íµ¥µÄ·½·¨£¬ÎÞÐèÖØÔØä¯ÀÀÆ÷´°¿Ú¡£jQuery ¿âÈÃÄúʹÓà Ajax ±íµ¥Ìá½»¹¦ÄܽøÒ»²½Ìṩһ¸ö·½±ã¿ì½ÝµÄ·½·¨£¬ÒÔÉÙÁ¿´úÂëÉú³É¿ÉÓà Ajax µÄ Web ±íµ¥¡£ÔÚ±¾ÎÄÖУ¬Ñ§Ï°ÈçºÎʹÓà jQuery ´´½¨»ù´¡ Ajax ±íµ¥Ìá½»£¬ÒÔ¼°ÈçºÎʹÓøü¼ÊõÑéÖ¤Ò»¸öÓû§¡£±¾ÎÄʹÓà jQuery ÑÝʾÁË Ajax Óû§×¢²á¼¼Êõ£¬±ÈÈ磬¼ì²éÓû§Ãû¿ÉÓÃÐÔ£¬ÒÔ¼°µ±Ñ¡ÔñµÄÓû§ÃûÒÑ´æÔÚʱÌáʾÓû§Ãû¡£¼È²»ÐèÒª±íµ¥Ìá½»Ò²ÎÞÐèÒ³ÃæÖØÔØ¡£Èç¹ûÄú¶Ô jQuery ²»ÊǺÜÊìϤ£¬Ëü±¾ÖÊÉÏÊÇÒ»¸ö JavaScript ¿â£¬Ê¹ JavaScript ¿ª·¢±äµÃºÜÈÝÒס£ËüʹËùÐèµÄ´úÂëÁ¿×îС»¯£¬ÒòΪËüÓÐÐí¶àÄÚÖù¦ÄÜ£¬ÕâÑùÄú¾Í²»ÔÙÐèҪΪÕâЩ¹¦Äܱàд¿Í»§¶Ëº¯Êý»ò¶ÔÏóÁË¡£
ʹÓà jQuery ½øÐÐ±íµ¥Ìá½»
ÎÞÐèÖØÔؼ´¿ÉÌá½»Ò»¸ö±íµ¥Ôںܶೡ¾°Öж¼ÊǺÜÓÐÓõġ£ÀýÈ磬ÓÐÁËËü£¬Äú¾Í¿ÉÒÔÔÚÌá½»±íµ¥Ö®Ç°Ê¹Óà JavaScript ´úÂëÑéÖ¤±íµ¥×ֶΣ¬À´ÔÚÒ»¸öµ¥Ò³ÃæÓ¦ÓóÌÐòÖÐÌá½»±íµ¥»òÕß - Èç±¾ÎÄËùʾ- È·¶¨ÊÇ·ñÓû§ÃûÒѾע²á¹ý¡£Ê¹Óà jQuery ´¥·¢Ò»¸ö±íµ¥Ìá½»ÓÐÁ½ÖÖ·½·¨£ºÊ¹Óà submit ´¦Àíº¯Êý»ò click ´¦Àíº¯Êý¡£Çåµ¥ 1 ÏÔʾÁËÈçºÎʹÓà submit ´¦Àíº¯ÊýÌá½»Ò»¸ö±íµ¥¡£
Çåµ¥ 1. ʹÓà jQuery µÄ submit ´¦Àíº¯ÊýÌá½»±íµ¥
<script type=¡°text/javascript¡± src=¡°¡±></script>
<script type=¡°text/javascript¡±>
$£¨document£©¡£ready£¨function£¨£© {
$£¨¡®#submitForm¡¯£©¡£submit£¨function£¨e£© {
alert£¨$£¨¡®#sample¡¯£©¡£attr£¨¡®value¡¯£©£©£»
return e.preventDefault£¨£©£»
}£©£»
}£©£»
</script>
<form id=¡°submitForm¡± method=¡°post¡±>
<input type=¡°text¡± name=¡°sample¡± id=¡°sample¡± value=¡°Enter something¡± />
<input type=¡°submit¡± id=¡°submitBtn¡± value=¡°Submit¡± />
</form>
Çåµ¥ 2 չʾÈçºÎʹÓà click ´¦Àíº¯ÊýÌá½»±íµ¥¡£
Çåµ¥ 2. ʹÓà jQuery µÄ click ´¦Àíº¯ÊýÌá½»±íµ¥
<script type=¡°text/javascript¡±
src=¡°¡±></script>
<script type=¡°text/javascript¡±>
$£¨document£©¡£ready£¨function£¨£© {
$£¨¡®#submitBtn¡¯£©¡£click£¨function£¨e£© {
alert£¨$£¨¡®#sample¡¯£©¡£attr£¨¡®value¡¯£©£©£»
return e.preventDefault£¨£©£»
}£©£»
}£©£»
</script>
<form id=¡°submitForm¡± method=¡°post¡±>
<input type=¡°text¡± name=¡°sample¡± id=¡°sample¡± value=¡°Enter something¡± />
<input type=¡°submit¡± id=¡°submitBtn¡± value=¡°Submit¡± />
</form>
ÕâÁ½¸öÇåµ¥»ù±¾ÉÏÊÇÒ»ÑùµÄ£ºËüÃǶ¼ÊÇǶÈë jQuery ¿âµÄ£¬ÔÚ·ÃÎÊÈκÎÔªËØ֮ǰʹÓà ready ´¦Àíº¯ÊýÈ·ÈÏÒ³Ãæ±»¼ÓÔØ£¬´¦Àíº¯Êý°üÀ¨ÏàͬµÄ´úÂ롣ΩһµÄ²»Í¬ÊÇ´¦Àíº¯ÊýºÍ·ÖÅä¸ø´¦Àíº¯ÊýµÄÔªËØ¡£submit ´¦Àíº¯ÊýÐèÒª·ÖÅäÒ»¸ö±íµ¥ÔªËØ£¬¶ø click ´¦Àíº¯Êý£¬Èκοɵã»÷µÄÔªËؼ´¿É - ±¾ÀýÖÐÊÇ Submit °´Å¥¡£ÎªÁ˱ÜÃâÌá½»±íµ¥Ê±Ë¢ÐÂÒ³Ã棬Äú±ØÐëʹÓà preventDefault º¯Êý¡£Òª·ÃÎÊ preventDefault º¯Êý£¬Äú±ØÐë´«µÝ´¦Àíº¯Êý£¨¼´Ê¹×÷Ϊһ¸ö²ÎÊý£©»òÕßʹÓÃËü·ÃÎʸú¯Êý¡£
¾¡¹ÜÒÔÉÏÁ½ÖÖÑ¡Ôñ¶¼ÊÇÓÐЧµÄ£¬µ« submit ´¦Àíº¯Êý¸üΪ³£ÓÃЩ¡£È»¶ø£¬ÓÐЩÇé¿öÏ£¬Äú¿ÉÄÜÓÖ²»Ö¹Ò»¸ö Submit °´Å¥£¬Õâ¾ÍÐèҪÿ¸ö°´Å¥Ò»¸ö click ´¦Àíº¯Êý¡£Çåµ¥ 3 չʾÁËÕâÑùÒ»¸ö±ØÐëʹÓà click ´¦Àíº¯ÊýµÄ³¡¾°£¬ÒòΪÁ½¸ö Submit °´Å¥¶¼ÄÜ´¥·¢±íµ¥Ìá½»¡£
Çåµ¥ 3. ʹÓÃÁ½¸ö submit °´Å¥Ìá½»±íµ¥
<script type=¡°text/javascript¡±
src=¡°¡±></script>
<script type=¡°text/javascript¡± src=¡°register.js¡±></script>
<div id=¡°container¡±>
<div id=¡°message¡±></div>
<form method=¡°post¡± id=¡°mainform¡±>
<label for=¡°username¡±>Username</label>
<input type=¡°text¡± name=¡°username¡± id=¡°username¡± value=¡°¡± />
<label for=¡°password¡±>Password</label>
<input type=¡°password¡± name=¡°password¡± value=¡°¡± />
<input type=¡°submit¡± name=¡°action¡± id=¡°login¡± value=¡°Log in¡± />
<h2>Extra options £¨registration only£©</h2>
<label for=¡°firstname¡±>First name</label>
<input type=¡°text¡± name=¡°firstname¡± value=¡°¡± />
<label for=¡°lastname¡±>Last name</label>
<input type=¡°text¡± name=¡°lastname¡± value=¡°¡± />
<label for=¡°email¡±>Email</label>
<input type=¡°text¡± name=¡°email¡± value=¡°¡± />
<input type=¡°submit¡± name=¡°action¡± id=¡°register¡± value=¡°Register¡± />
</form>
</div>
×¢Ò⣬ÔÚ±¾ÀýÖÐÕâ¸ö±íµ¥¿ÉÒÔÖ´Ðжà¸ö»î¶¯£ºÏÖÓÐÓû§¿ÉÒԵǼ£¬ÐÂÓû§¿ÉÒÔͨ¹ýÊäÈ븽¼ÓÕË»§ÐÅÏ¢½øÐÐ×¢²á¡£Ê¹ÓÃ±íµ¥É쵀 submit ´¦Àíº¯ÊýÔÚÕâÖÖ³¡¾°Öв»ÄÜÔËÐУ¬ÒòΪËü²»ÄÜÈ·¶¨Äĸö°´Å¥´¥·¢±íµ¥Ìá½»¡£Òò´Ë£¬Çåµ¥ 4 ʹÓà click ´¦Àíº¯ÊýÀ´È·¶¨Ã¿¸ö°´Å¥²ÉȡʲôÐж¯£¬±ãÓÚÄúÒÔºóÒÀ´Ë´¦ÀíÊý¾Ý¡£
Çåµ¥ 4. register.js ÖÐÌá½»°´Å¥µÄ Click ´¦Àíº¯Êý
$£¨document£©¡£ready£¨function£¨£© {
$£¨¡°#register, #login¡±£©¡£click£¨function£¨e£© {
var name = £¨$£¨event.target£©¡£attr£¨¡®id¡¯£© == 'register¡®£© ? 'Registration' : 'Login';
return e.preventDefault£¨£©£»
}£©£»
}£©£»
Îĵµ×¼±¸ºÃºó£¬ÄúÐèҪΪ Register ºÍ Login °´Å¥·ÖÅä click ´¦Àíº¯Êý¡£click ´¦Àíº¯Êý½ÓÊÕÒ»¸ö²ÎÊý£¬ÃüÃûΪ e£¨×÷Ϊʼþ£©¡£´Ëʼþ¶ÔÏóÉÔºóÓÃÀ´Ô¤·ÀĬÈÏ±íµ¥Ìá½»¡£ÕýÈç֮ǰ´úÂëËùÊö¡£µ± click ´¦Àíº¯Êý±»µ÷ÓÃʱ£¬µ±Ç°±»µã»÷µÄ¶ÔÏóµÄ ID ±»·ÃÎÊ£¬ÓÃÀ´È·¶¨ÕâÊÇÒ»¸öÓû§µÇ¼»¹ÊÇÒ»¸öÐÂÓû§×¢²á¡£
ÏÖÔÚ£¬ÄúÒѾ֪µÀÁËʹÓà jQuery ÈçºÎÌá½»±íµ¥£¬ÎÒÃÇÀ´¿´¿´Ê¹Óà jQuery ÖÐµÄ Ajax ºÍ PHP ÈçºÎÈÏÖ¤Ò»¸öÓû§¡£
[1] [2] [3] [4] ÏÂÒ»Ò³
¡¾Ôð±à:peter¡¿
¡¡