根据选择菜单选项创建 cookie?
我试图创建一个选择菜单,当用户选择一个选项并单击“继续”按钮时,会使用该选项的内容创建一个 cookie。下面的代码可以工作,但它只创建默认的“a”cookie,而不是根据用户选择的选项进行更改。
<select id='select_letter'>
<option>a</option>
<option>b</option>
</select>
<a href='/' id='continue'><div id='continue_button'></div></a>
<script>
$(document).ready(function(){
var singleValues = $("#select_letter").val();
$('#continue').click(function() {
$.cookie("project", singleValues);
})
});
</script>
我尝试将 onclick 事件绑定到继续按钮,以获取已选择的任何选项,但失败了。有什么想法吗?谢谢!
<script>
$('option').live("click", function(){
var singleValues = $("#select_letter").val();
$('#continue').click(function() {
$.cookie("project", singleValues);
})
});
</script>
I'm trying to create a select menu in a way that when users select an option and click on the continue button, a cookie is created with the contents of the option. The code directly below works, but it only creates the default "a" cookies rather than changing based on what option the user chooses.
<select id='select_letter'>
<option>a</option>
<option>b</option>
</select>
<a href='/' id='continue'><div id='continue_button'></div></a>
<script>
$(document).ready(function(){
var singleValues = $("#select_letter").val();
$('#continue').click(function() {
$.cookie("project", singleValues);
})
});
</script>
I've tried to tie an onclick event to the continue button that gets whatever option has been selected, but it's been a fail. Any ideas? Thanks!
<script>
$('option').live("click", function(){
var singleValues = $("#select_letter").val();
$('#continue').click(function() {
$.cookie("project", singleValues);
})
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在单击事件之外分配选定的值。你需要在里面做。您在加载时存储默认值,然后从不更新它。
You assign the selected value outside of the click event. You need to do it inside. You are storing the default value on load and then never updating it.
在您选择的选项中,您没有分配值。
例如
应该是
示例代码: http://jsfiddle.net/AMefb/
In your select options, you are not assigning values.
e.g.
should be
example code: http://jsfiddle.net/AMefb/