1

I need to get a current value of SELECT tag. And for this I'm using $('select').val(), but this return only default value and it's don't changed. May be I can help me?

$('div#buy input').fancybox({
    ajax : {
        type    : "POST",
        data    : {
            id: $('input[name="id"]').val(),
            count: $('#count').val()
        }
    }
});

<select id="count">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
</select>
7
  • You must be doing something else wrong, because it works: jsfiddle.net/aNQJY Commented Jul 7, 2011 at 17:23
  • 1
    The problem is that fancybox probably runs on page load, so the value is read on page load and then it is of course the default one. You have to read the value when it is changed. Commented Jul 7, 2011 at 17:27
  • Mine script is a last loading. Commented Jul 7, 2011 at 17:32
  • I think you did not understand what I mean. When do you call $('div#buy input').fancybox(...)? Do you call it after the uses selects the value or before that ? Commented Jul 7, 2011 at 17:36
  • I've uses select just in .fancybox(), not after Commented Jul 7, 2011 at 17:43

8 Answers 8

5

$('#count').val() will return current selected value. you are missing values for option.

<select id="count">
<option value="1">1</option>
<option value="2">2</option>........

Edit :

If you don't want to specify value in option than you can use.

$('#count :selected').val() 
Sign up to request clarification or add additional context in comments.

7 Comments

You don't need the values. If the attribute is not set, the text is used as value.
You're right that he can just use $('#count') however it's not because he's missing values for options. Values are optional.
@Felix Kling, Jamie Dixon . Thanks for clarification.
$('#count').val() already works without any modifications. The problem is that this is executed on page load.
@Nill on domready selected will be default only . if you want to update your fancy box with selected value than attach a event on change. and than update your fancy box by getting $("count").val()
|
3

Try this:

$('select option:selected').val();

Comments

3

You can do use the :selected selector on dropdowns to get the currently selected item:

var selectedValue = $('#count :selected').val()

UPDATE

Here's a quick fiddle of this working: http://jsfiddle.net/deYmN/

4 Comments

Your selector is a little off. It should be 'select :selected' (with a space between "select" and ":selected").
This should not work. :selected only works for option elements.
@JamieDixon: Well, before the edit of course ;) #count:selected will not work.
Ah I see your point now Felix. Sorry. You're right. I missed the space initially. Good spot.
2

In jQuery, $('#count').val() will give the currently selected value for the <select> element with id "count". I suspect that your code sample is running before the user has made a selection, and that's why you're getting the default value. You should set your count property sometime after the user has made a selection. Something like this will work:

var fancyboxOptions = {
    ajax : {
        type    : "POST",
        data    : {
            id: $('input[name="id"]').val(),
            count: $('#count').val()
        }
    }
}

$(document).ready(function() {
    $('div#buy input').fancybox(fancyboxOptions);
    $('#count').change(function() {
        fancyboxOptions.ajax.data.count = $('#count').val();
    });
});

This way, the options object will get updated when the selection is changed.

3 Comments

It's work, but just particual. AJAX sent only count value. Have any ideas about id?
I'm sure it's having the same problem. You need to add a change event handler or some equivalent for it as well.
Tnx for advice, I'm doing this now ^_^
1

Are you running the fancybox/ajax before the user changes the select?

It works fine here:

http://jsfiddle.net/ERfFA/

1 Comment

Well when the script loads it picks up the first value (the default). You have to run the script after a user selects what he wants.
1

Use:

$('select :selected').val()

Source

Comments

1
$("#count option:selected").val();
//Use text to get the text presented on the ui
$("#count option:selected").text();

Working example: http://jsfiddle.net/AKmHU/

Comments

1

The id of your select is "count", so you need do something like this:

$('#count:selected').val()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.