Чтобы отобразить выбранное значение в поле выбора с помощью jQuery, вы можете использовать различные методы. Вот несколько примеров:
Метод 1: использование функции val()
// HTML
<select id="mySelect">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
// jQuery
$(document).ready(function() {
var selectedValue = "2"; // Set the selected value dynamically
// Set the selected value in the select box
$("#mySelect").val(selectedValue);
});
Метод 2: использование функции prop()
// HTML
<select id="mySelect">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
// jQuery
$(document).ready(function() {
var selectedValue = "3"; // Set the selected value dynamically
// Set the selected value in the select box
$("#mySelect option[value='" + selectedValue + "']").prop("selected", true);
});
Метод 3. Использование функции attr()
// HTML
<select id="mySelect">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
// jQuery
$(document).ready(function() {
var selectedValue = "1"; // Set the selected value dynamically
// Set the selected value in the select box
$("#mySelect option[value='" + selectedValue + "']").attr("selected", "selected");
});