1. 程式人生 > >Jquery外掛知識之Jquery.cookie實現頁面傳值

Jquery外掛知識之Jquery.cookie實現頁面傳值

<span style="color:#ff0000;">//login.html頁面</span>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文件</title>
<script src="jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="jquery.cookie.js" type="text/javascript"></script>
<script>
	$(document).ready( 
		function(){
			$("#btnOk").click(
				function(){
					//存放剛才的zhang.laoshi資訊;
					//$.cookie('user',$("#user").val()); //user:存放資訊key;value
					//alert($("#meiniu").attr("src"));
					$.cookie('src',$("#meiniu").attr("src"));
				}
			);	
		}
	);
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="success.html" ><!--action提交到某個頁面;get:提交方式?user='zhang' and pwd='123'-->
  <table width="800" border="0" align="center">
    <tr>
      <td width="65">使用者:</td>
      <td width="613"><input type="text" name="user" id="user" /></td>
      <td width="108"> </td>
    </tr>
    <tr>
      <td>圖片</td>
      <td><img src="meiniu.jpg" width="118" height="79" id="meiniu" /></td>
      <td> </td>
    </tr>
    <tr>
      <td><input type="submit" name="button" id="btnOk" value="提交" /></td>
      <td><input type="reset" name="button2" id="button2" value="重置" /></td>
      <td> </td>
    </tr>
  </table>
</form>
</body>
</html>


//2.提交之後的success.html頁面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文件</title>
<script src="jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="jquery.cookie.js" type="text/javascript"></script>
<script>
$(document).ready(
	function(){
		//顯示到div裡面;
		//$("#content").html($.cookie('user')); //html:相當於div的innerHTML
		var s=$.cookie('src');
		$("#meiniu").prop("src",s);
	}
);

</script>
</head>

<body>

<div id="content" style="border:1px solid #f00; width:600px; height:300px;">
<img id="meiniu" src="" width="118" height="79"/>
</div>
</body>
</html>