Ich versuche, einen Standardwert für ein Dropdown-Menü festzulegen, das ich habe. Das Dropdown enthält alle 12 Monate. Es ist dynamisch gefüllt. Allerdings versuche ich es so zu machen, dass der Standard-Dropdown-Wert der aktuelle Monat ist. Ich verwende KnockoutJSLegen Sie den Standardwert für ein Dropdown-Feld fest
Der folgende Code ist, wie ich Informationen zum Dropdown-HTML-Code sende.
self.setMonthData = (data: any) => {
var len = data.List.length,
i;
var monthData = [];
for (i = 0; i < len; i++) {
self.monthData.push(
{
name: data.List[i].month,
month: data.List[i].monthNumber
}
)
}
}
HTML-Code:
<select id="monthSelect" class="select select2" style="margin: 10px;"
data-bind="options: monthData,
optionsText: 'name',
optionsValue: 'month'">
<option></option>
</select>
Dieser Code ist, wie die Seite geladen, die das Drop-Down-Funktionen.
$(function() {
ko.applyBindings(viewModel);
getGridData(initialMonth, initialYear);
viewModel.load();
$("#monthSelect").on('change', function() {
var month = $('#monthSelect').find("option:selected").val();
var year = $('#yearSelect').find("option:selected").val();
viewModel.loadChangesData(month, year);
getGridData(month, year);
});
$("#yearSelect").on('change', function() {
var month = $('#monthSelect').find("option:selected").val();
var year = $('#yearSelect').find("option:selected").val();
viewModel.loadChangesData(month, year);
getGridData(month, year);
});
});