在jQuery中获取SELECT的值和文本[复制]

时间:2022-12-09 17:51:06

Possible Duplicate:
Getting the value of the selected option tag in a select box

可能的重复:在选择框中获取所选选项标记的值

For a SELECT box, how do I get the value and text of the selected item in jQuery?

对于选择框,如何使用jQuery获取所选项的值和文本?

For example,

例如,

<option value="value">text</option>

5 个解决方案

#1


86  

<select id="ddlViewBy">
    <option value="value">text</option>
</select>

JQuery

JQuery

var txt = $("#ddlViewBy option:selected").text();
var val = $("#ddlViewBy option:selected").val();

JS Fiddle DEMO

JS小提琴演示

#2


12  

$("#yourdropdownid option:selected").text(); // selected option text
$("#yourdropdownid").val(); // selected option value

#3


8  

$('select').val()  // Get's the value

$('select option:selected').val() ; // Get's the value

$('select').find('option:selected').val() ; // Get's the value

$('select option:selected').text()  // Gets you the text of the selected option

Check FIDDLE

检查小提琴

#4


6  

You can do like this, to get the currently selected value:

您可以这样做,以获取当前选择的值:

$('#myDropdownID').val();

& to get the currently selected text:

&获取当前选定文本:

$('#myDropdownID:selected').text();

#5


4  

on the basis of your only jQuery tag :)

基于您唯一的jQuery标记:)

HTML

HTML

<select id="my-select">
<option value="1">This is text 1</option>
<option value="2">This is text 2</option>
<option value="3">This is text 3</option>
</select>

For text --

对于文本,

$(document).ready(function() {
    $("#my-select").change(function() {
        alert($('#my-select option:selected').html());
    });
});

For value --

值,

$(document).ready(function() {
    $("#my-select").change(function() {
        alert($(this).val());
    });
});

#1


86  

<select id="ddlViewBy">
    <option value="value">text</option>
</select>

JQuery

JQuery

var txt = $("#ddlViewBy option:selected").text();
var val = $("#ddlViewBy option:selected").val();

JS Fiddle DEMO

JS小提琴演示

#2


12  

$("#yourdropdownid option:selected").text(); // selected option text
$("#yourdropdownid").val(); // selected option value

#3


8  

$('select').val()  // Get's the value

$('select option:selected').val() ; // Get's the value

$('select').find('option:selected').val() ; // Get's the value

$('select option:selected').text()  // Gets you the text of the selected option

Check FIDDLE

检查小提琴

#4


6  

You can do like this, to get the currently selected value:

您可以这样做,以获取当前选择的值:

$('#myDropdownID').val();

& to get the currently selected text:

&获取当前选定文本:

$('#myDropdownID:selected').text();

#5


4  

on the basis of your only jQuery tag :)

基于您唯一的jQuery标记:)

HTML

HTML

<select id="my-select">
<option value="1">This is text 1</option>
<option value="2">This is text 2</option>
<option value="3">This is text 3</option>
</select>

For text --

对于文本,

$(document).ready(function() {
    $("#my-select").change(function() {
        alert($('#my-select option:selected').html());
    });
});

For value --

值,

$(document).ready(function() {
    $("#my-select").change(function() {
        alert($(this).val());
    });
});