我正在从 5 个选项卡表单中获取所有输入。当循环浏览它们时,我将它们添加到评论部分。我让它工作正常,但我想显示选择下拉列表的文本,而不是显示值,因为该值对用户来说没有意义。这是我正在使用的一些代码片段。这是内部和 $(:input.each(function(){} 我检查它是否是一个选择。如果是,我设置一个 tmp 变量并将“_in”添加到名称中,因为我有所有字段名称加上该变量在确认选项卡上。然后我检查新变量的长度以确保它在确认选项卡上。如果是这样,请将 html 更新为文本。问题是它显示所有下拉文本选项。如果我这样做 .val( )它只给出所选值。
//Gives all text options from the drop down.
if ($(this).prop('type') == 'select-one') {
var tmp = '#'+$(this).attr('name')+'_in';
if ($(tmp).length > 0) {
$(tmp).html($(this).text());
}
}
//Gives only value of selected option from drop down.
if ($(this).prop('type') == 'select-one') {
var tmp = '#'+$(this).attr('name')+'_in';
if ($(tmp).length > 0) {
$(tmp).html($(this).val());
}
固定解决方案
if ($(this).prop('type') == 'select-one') {
var tmp = '#'+$(this).attr('name')+'_in';
var tmp2 = '#'+$(this).attr('name')+' option:selected';
if ($(tmp).length > 0) {
tmp3 = $(tmp2).text();
$(tmp).html(tmp3);
}
}
最佳答案
您可以使用以下方法获取选择的文本
$("#yourdropdownid option:selected").text();
关于jquery - 循环浏览每个输入时从 select 获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11544738/