gpt4 book ai didi

jquery - 在下拉框中设置默认选定的项目

转载 作者:行者123 更新时间:2023-12-01 06:13:23 24 4
gpt4 key购买 nike

我需要将第一个选定的项目设置为下拉框的索引 1。我该怎么做?

我已经尝试过类似下面的方法。但是我需要在不使用硬编码值的情况下(即没有“日营”)来做到这一点。

 //for set default values
$("#ddlServiceCategoryType option").each(function () {
if ($(this).text() == 'Day Camp') {
$(this).attr('selected', 'selected');
return false;
}
});

enter image description here

HTML

<select id="ddlServiceCategoryType" name="ServiceCategoryTypeViewModel.ServiceCategoryType.Id" class="has_sb" style="display: block;"><option value="">-Please select a service category-</option>
<option value="1">Day Camp</option>
<option value="2">Boarding</option>
<option value="3">Pet Sitting</option>
<option value="4">Grooming</option>
</select>

最佳答案

尝试

$("#ddlServiceCategoryType").val(function () {
return $(this).find('option:eq(1)').attr('value')
});

演示:Fiddle

关于jquery - 在下拉框中设置默认选定的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18850392/

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