获取选定的文本并将其放入变量中

发布于 2024-09-14 13:43:13 字数 1423 浏览 0 评论 0原文

我有以下代码,它加载 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再可℃爱ぅ一点好了 2024-09-21 13:43:13

要获取所选文本,您可以使用:selected.text( ),像这样:

$("#mySelect :selected").text();

(注意空格,它是一个子元素,所以很重要)

或者,在当前的 .change() 处理程序,它看起来像这样:

$(this).children(":selected").text();

To get the text of the selected <option> you could use :selected and .text(), like this:

$("#mySelect :selected").text();

(Note the space, it's a child so it's important)

Or, inside your current .change() handler it would look like this:

$(this).children(":selected").text();
泪是无色的血 2024-09-21 13:43:13

var selectedOptionValue = $('#mySelect:selected').val();
var selectedOptionText = $('#mySelect:selected').html();

var selectedOptionValue = $('#mySelect:selected').val();
var selectedOptionText = $('#mySelect:selected').html();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文