开发者

Getting value in <select>, using JQuery

I need to get a current value of SELECT tag. And for this I'm usin开发者_开发知识库g $('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>


$('#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() 


Try this:

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


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/


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.


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

It works fine here:

http://jsfiddle.net/ERfFA/


Use:

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

Source


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

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


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜