2013-11-02 14 views
1

我有汽車模型的列表,我試圖插入的值的選擇cookie與jQuery。選擇汽車的名單,插入價值cookie - jQuery

這是我的代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>test</title> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script> 
<script src="jquery.cookie.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    var singleValues = $("#car").val(); //I NEED TO BE INSIDE 
    $('#continue').click(function() { 
     $.cookie("car", singleValues); 
    }) 

    $('#continue').click(function() { 
     var singleValues = $("#car").val(); 
     $.cookie("car", singleValues); 
    }) 
</script> 
</head> 
<body> 
<select id='car'> 
    <option value="mazda">mazda</option> 
    <option value="honda">honda</option> 
</select> 
<a href='/' id='continue'>#<div id='continue_button'></div></a> 
</body> 
</html> 

但餅乾didnt做....爲什麼?

非常感謝你的幫助:)

回答

0

你需要用你的代碼在文檔準備好處理程序。試試這個:

<script type="text/javascript"> 
    $(function() { 
     $('#continue').click(function() { 
      var singleValues = $("#car").val(); 
      $.cookie("car", singleValues); 
     }) 
    }); 
</script> 

當前你的代碼是在元素存在於DOM之前附加事件。