选择列表的汽车,将值插入饼干 - jQuery

select car of list, insert value to cookies- jQuery

本文关键字:插入 饼干 jQuery 列表 汽车 选择      更新时间:2023-09-26

我有汽车型号列表,我试图用jQuery将select的值插入到cookie中。

这是我的代码:

<!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>

但是饼干没有做...为什么?

非常感谢您的帮助:)

您需要将

代码包装在文档就绪处理程序中。试试这个:

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

目前,您的代码在元素存在于 DOM 中之前附加事件。