简体   繁体   English

如何在Flask(Python)中更新具有选择选项的表单

[英]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.

相关问题 如何在不停止脚本和等待用户表单输入的情况下,将变量从Flask Form更新为连续运行的python脚本? - How do I update variable from Flask Form to a continuously running python script, without the script stopping and waiting for the user form input? 如何使用Python Flask重置HTML表单? - How do I reset my HTML form with Python Flask? Flask WTForms:如何将表单值返回给Python? - Flask WTForms: how do I get a form value back into Python? 如何使用Flask填充选择标记? - How do I populate a select tag with Flask? Flask SQL Alchemy - 如何通过表单一次更新多条记录 - Flask SQL Alchemy - How do I update several records at once through a form 如何在Python中更新字典值,让用户选择要更新的密钥,然后选择新值? - How do I update a dictionary value having the user choose the key to update and then the new value, in Python? 使用 Flask POST 后,选择选项保持选中状态 - Having a select option stay selected after POST with Flask 如何使用 Python、Flask 和 SQLite3 实现下拉 select 选项的动态相关更改? - How do I implement dynamic dependent change of dropdown select options, using Python, Flask and SQLite3? 如何在另一个模板中呈现模板表单。 Python、Flask、Jinja2 - How do I render a template form inside another template. Python, Flask, Jinja2 如何将数据库 ID 从 flask html 传递到带有 python 的帖子表单中 - How do I pass the database id from flask html into a post form with python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM