简体   繁体   English

如何在Flask Python中获取drop box的值

[英]How to get value on drop box in Flask Python

I have a file XML. 我有一个文件XML。 It's follow . 接下来。 I use Flask Framework Python to get query in search bar and value of current dropbox (eg english or vietnamese). 我使用Flask Framework Python在搜索栏中获取查询以及当前dropbox的值(例如英语或越南语)。 I use "POST" method get value when I enter or click "Search" button. 我在输入时使用“POST”方法获取值或单击“搜索”按钮。 But value of dropbox is null. 但dropbox的值为null。

 <form class="input-group">

                 <div class="input-group-btn search-panel" id="menu1">
                        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                            <span>Language</span>
                        </button>
                        <ul name ="btnLanguage" class="dropdown-menu" role="menu">
                            <li><a>English</a></li>
                            <li><a>Vietnamese</a></li>
                        </ul>
                 </div>

                <input type="text" class="form-control" name="query" id ="query" placeholder="Search sentence...">
                <span class="input-group-btn">
                    <button id ="btnSearch" class="btn btn-success" type="submit">
                        <span class="glyphicon glyphicon-search" aria-hidden="true"></span>
                    </button>
                </span>
            </form>

How can you help me. 你怎么能帮我 Thank you 谢谢

 @app.route("/search" , methods=["POST"])
 def search():
    print("True")
    # read the posted values from the UI
    _query = request.form['query']
    print(_query)
    _language = request.value['btnLanguage']
    print(_language)

and Json Method: 和Json方法:

$(function(){
$('#btnSearch').click(function(){
    $.ajax({
        url: '/search',
        data: $('form').serialize(),
        type: 'POST',
        success: function(response){
            console.log(response);
        },
        error: function(error){
            console.log(error);
        }
    });
});

}); });

It is because ul-li is not a regular form control (Checkout this link for available form control in html). 这是因为ul-li不是常规表单控件(在html中查看此链接以获取可用的表单控件)。 So jquery will not consider it when serializing form data. 所以jquery在序列化表单数据时不会考虑它。

Solution : You need to write a script to append custom data with your form data or you should use <select> instead of ul-li dropdown. 解决方案 :您需要编写一个脚本来附加表单数据的自定义数据,或者您应该使用<select>而不是ul-li下拉列表。


Other problems with your code. 您的代码的其他问题。

  • You are using click() event handler with a button having type submit so two events will occur, one is ajax you are calling and another is form will be submitted to server side with GET. 您正在使用带有类型提交的按钮的click()事件处理程序,因此将发生两个事件,一个是您正在调用的ajax,另一个是表单将通过GET提交给服务器端。 To remove is issue you should be listening for form submit() event with preventing event propagation (check this answer ). 要删除是问题,您应该监听表单submit()事件以防止事件传播(检查此答案 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM