How can I set the option value of a drop-down list to the option selected by the user. I believe I need to use a script but I am unsure as to the best practice. The options are populated via JSON response. This functionality is needed for form submission.
<div class="select">
<select name="selectcompanies" id="selectcompanies">
<option value="">Company</option>
{% for company in companies %}
<option value="" name="selected_company" id="selected_company">{{ company.name }}</option>}
{% endfor %}
</select>
</div>
To make a select option as "selected" we can use the "selected" html attribute.
While looping the json value,
It will make that option appear as selected.
You can refer to https://www.w3schools.com/tags/att_option_selected.asp for more details.
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.