获取选定的文本并将其放入变量中
我有以下代码,它加载 xml 文件以填充下拉列表。我想将选定的选项文本放入变量中。我已经得到了选择的值,但没有得到文本。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" media="all" href="style.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.ajax({
type: "GET",
url: "make10.xml",
dataType: "xml",
success: function(xml) {
var select = $('#mySelect');
var optionsHtml = new Array();
$('make', xml).each(function(){
var value = $(this).attr('value');
var label = $(this).text();
optionsHtml.push( "<option class='ddindent' value='"+ value +"'>"+label+"</option>");
});
optionsHtml = optionsHtml.join('');
select.append(optionsHtml);
select.children(":first").text("Select Make").attr("selected",true);
}
});
$('#mySelect').change(function() {
var val=$(this).val();
alert(val);
});
});
</script>
</head>
<body>
<div id="page-wrap">
<select id="mySelect">
<option>loading</option>
</select>
</div>
</body>
</html>
I have the following code which loads a xml file to populate a dropdown. I would like to put the selected options text into a variable. I already got the value of the selection but not the text.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" media="all" href="style.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.ajax({
type: "GET",
url: "make10.xml",
dataType: "xml",
success: function(xml) {
var select = $('#mySelect');
var optionsHtml = new Array();
$('make', xml).each(function(){
var value = $(this).attr('value');
var label = $(this).text();
optionsHtml.push( "<option class='ddindent' value='"+ value +"'>"+label+"</option>");
});
optionsHtml = optionsHtml.join('');
select.append(optionsHtml);
select.children(":first").text("Select Make").attr("selected",true);
}
});
$('#mySelect').change(function() {
var val=$(this).val();
alert(val);
});
});
</script>
</head>
<body>
<div id="page-wrap">
<select id="mySelect">
<option>loading</option>
</select>
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要获取所选
的文本,您可以使用
:selected
和.text( )
,像这样:(注意空格,它是一个子元素,所以很重要)
或者,在当前的
.change()
处理程序,它看起来像这样:To get the text of the selected
<option>
you could use:selected
and.text()
, like this:(Note the space, it's a child so it's important)
Or, inside your current
.change()
handler it would look like this:var selectedOptionValue = $('#mySelect:selected').val();
var selectedOptionText = $('#mySelect:selected').html();
var selectedOptionValue = $('#mySelect:selected').val();
var selectedOptionText = $('#mySelect:selected').html();