문제

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>
도움이 되었습니까?

해결책

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.

    var singleValues = $("#select_letter").val(); //I NEED TO BE INSIDE
    $('#continue').click(function() {
        $.cookie("project", singleValues);
    })

    $('#continue').click(function() {
        var singleValues = $("#select_letter").val();
        $.cookie("project", singleValues);
    })

다른 팁

In your select options, you are not assigning values.

e.g.

<select id='select_letter'>
    <option>a</option>
    <option>b</option>
</select>

should be

<select id='select_letter'>
    <option value="a">a</option>
    <option value="b">b</option>
</select>

example code: http://jsfiddle.net/AMefb/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top