英文:
how to save selected option in select after send form
问题
我在表单中选择了一个选项,并将该值发送到我的后端。我想知道是否有办法保存我发送的值在我的选择框中,以免它切换到默认选项。
英文:
I have select in form and i send value from option to my backend
I was wondering if there's way to save value that i've send in my select
template.html
<form method="get" class="mb-3">
<div class="row">
<div class="col-8 w-25 custom-select">
<form method="get" class="w-50 custom-select">
<select name='ObjectGUID' class="form-select" id="select2">
<option value="" disabled selected>Выберите объект:</option>
{% for obj in objects %}
<option value="{{ obj.GUID }}">{{ obj }}</option>
{% endfor %}
<option value="1">1</option>
<option value="2">2</option>
</select>
<button type="submit" class="mb-3 ms-2 mt-3 btn btn-outline-primary w-50">Применить</button>
</form>
</div>
</div>
</form>
After i send get request
答案1
得分: 1
你需要验证是否已经为此字段保存了一个值,然后可以在选择的选项上设置一个selected属性。例如:
<select name='ObjectGUID' class="form-select" id="select2">
<option {% if savedValue == '' %}selected{% endif %}>请选择对象:</option>
{% for obj in objects %}
<option {% if savedValue == obj.GUID %}selected{% endif %} value="{{ obj.GUID }}">{{ obj }}</option>
{% endfor %}
</select>
这里的 savedValue
是您在提交表单上保存的变量。
英文:
You need verify if there's already a value saved for this field, then you can set a selected attribute on your select's option. For exemple:
<select name='ObjectGUID' class="form-select" id="select2">
<option {% if savedValue == '' %}selected{% endif %}>Выберите объект:</option>
{% for obj in objects %}
<option {% if savedValue == obj.GUID %}selected{% endif %} value="{{ obj.GUID }}">{{ obj }}</option>
{% endfor %}
</select>
This savedValue
is the var you have saved on your submit form.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论