gpt4 book ai didi

javascript - 如何在通过 jQuery 附加选项后选择选择框中的第一个选项

转载 作者:太空宇宙 更新时间:2023-11-04 16:02:32 27 4
gpt4 key购买 nike

我在模式中有一个选择框,我在其中使用 jQuery 附加选项。我希望在附加所有项目后选择第一个选项。我尝试了一些但没有运气。以下是我的尝试。

我的模态选择框:

<select id="modal_cmbSection" name="modal_cmbSection" class="form-control required">
</select>

我的 jQuery 代码,其中附加并尝试设置选定的第一个选项:

var sectionOption = "";
var $sectionSelect = $('#modal_cmbSection');
$sectionSelect.empty();
$(".section-block").each(function() {
var eachSectionName = $(this).find(".section-section-name").val();
sectionOption = "<option>" + eachSectionName + "</option>";
$sectionSelect.append(sectionOption);
});
$sectionSelect.find('option:eq(0)').prop('selected', true);

最佳答案

只需这样做,

var sectionOption = "";
var $sectionSelect = $('#modal_cmbSection');
$sectionSelect.empty();
$(".section-block").each(function() {
var eachSectionName = $(this).find(".section-section-name").val();
sectionOption = "<option>" + eachSectionName + "</option>";
$sectionSelect.append(sectionOption);
});
//set selected as true for first option
$("#modal_cmbSection option:first-child").attr("selected", true);

关于javascript - 如何在通过 jQuery 附加选项后选择选择框中的第一个选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40887734/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com