[英]How do I update a form having select option in Flask (Python)
My question is about how to update select option in flask. 我的问题是关于如何更新烧瓶中的选择选项。 I have created a registration form which contains select option to select Department . 我创建了一个注册表,其中包含用于选择Department的选择选项。 There are four options in department , the selected value is getting stored in MySQL database. 部门中有四个选项,所选值将存储在MySQL数据库中。 This is working perfectly. 这很正常。 Now I need update user page where user can update their details. 现在,我需要更新用户页面,用户可以在其中更新其详细信息。 The options are static and not fetching from database, only selected value is getting stored. 这些选项是静态的,不是从数据库中获取的,仅存储选定的值。 Is it possible to get the form select option as pre-selected? 是否可以将表单选择选项设为预先选择的?
<div class="input-group" style="margin-bottom: 5px;">
<label class="input-group-addon">Department:<span style="color: red">*</span> </label>
<select name='employee_status' class="form-control">
<OPTION selected value='Sales'>Sales</OPTION>
<OPTION value='Marketing'>Marketing</OPTION>
<OPTION value='Support'>Support</OPTION>
<OPTION value='Development'>Development</OPTION>
</select>
</div>
Thanks in advance. 提前致谢。
If you want to update your html code with Flask you can, but you have to return a new html template and I don't think that the good way. 如果您想使用Flask更新html代码,则可以,但是您必须返回一个新的html模板,我认为这不是好方法。
You can make an AJAX call to your Flask route, and on the success callback you can update directly your form with the values you want (Easier with jquery). 您可以对Flask路线进行AJAX调用,并在成功回调中可以直接使用所需的值更新表单(使用jquery更容易)。
$.ajax({
type : 'POST',
url : '/updateForm',
dataType : "json",
contentType: 'application/json;charset=UTF-8',
data : JSON.stringify(dict_var),
success : function (result) {
$('.'+dict_var.form_name[result.value]).attr('selected','selected');
},
error : function (error) {
console.log(error);
}
});
With dict_var = { 'name' : form_name, 'value' : selected_value } 使用dict_var = {'name':form_name,'value':selected_value}
Something like this :) 像这样的东西:)
Peace 和平
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.