我们能否使用jQuery“动态选项值”获取选择菜单中所选选项的文本?

26 浏览
0 Comments

我们能否使用jQuery“动态选项值”获取选择菜单中所选选项的文本?

这个问题已经有了答案:

使用jQuery从下拉列表(select框)获取选定的文本

我想要显示下拉列表中选项的文本,但这些值是动态的,正确的方法是什么,现在只有id值出现了。


脚本如下:

var joblocation = $('#joblocation').find(":selected").text();
$("#joblocation1").html(joblocation);

我想在这里显示下拉列表选定的文本:

 

admin 更改状态以发布 2023年5月22日
0
0 Comments

你可以尝试这样做

 var optionText = $("#joblocation option:selected").text() //gets you the text from the selected option

然后像这样将变量添加到 span 中:

$("#joblocation1").text(optionText);

0
0 Comments

请尝试以下代码:

//This is for debugging and will show you the selected option object in console
console.info($("#joblocation option:selected") );
$("#joblocation1").html($("#joblocation option:selected").text() );

0