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小提琴演示
#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
检查小提琴
#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小提琴演示
#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
检查小提琴
#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());
});
});