I have a select box in a modal where I am appending options using jquery. I want the first option to be selected after appending all item. I have tried some but no luck. Can anyone please help me on this please? Here are my attempt below :
My select box in modal >>>
<select id="modal_cmbSection" name="modal_cmbSection" class="form-control required">
</select>
My jquery code where appending and trying to set selected first option >>>
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);
Simply do this,
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);
你能试一下吗:
$sectionSelect.find( 'option:first' ).prop( 'selected', true );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.