简体   繁体   English

不能使用 Flask 从 HTML 获得复选框的值

[英]Can't have the value of a check box from HTML using Flask

I'm facing a problem while trying to use a check box defined in my HTML code !我在尝试使用 HTML 代码中定义的复选框时遇到问题! The check box is used to select RegEx search !该复选框用于 select RegEx 搜索!

Here a screenshot of the Layout:这是布局的屏幕截图: 在此处输入图像描述

This is the HTML code:这是 HTML 代码:

<div class="filter_1">
    Filter :
</div>


<div class="RegEx_1">
    <input type="checkbox"  name="check">
    <label for="RegEx_1"> RegEx</label>
</div>

<form class ="search_1" name="search" action="/search" method="GET">
    <input type="text" placeholder="Search.." name="search_11">
    <input class="btn_1" type="submit" value="Submit">
</form>

The python code (route to call): python 代码(调用路径):

@app.route('/search', methods =["GET", "POST"])
def search():
results1=[]
res=""
q = request.args.get('search_11')
regex_check = request.form.get('check')

if regex_check : 
    res = "the box is checked"

elif q != "" :
    for i in kitt_list:
        if q in i :
            results1.append(i)
    for j in range(len(results1)):
        res = res + "\n" + results1[j]

return render_template('search.html', res=res, q=q)

Here are also the "search.html":这里还有“search.html”:

<div class="title_1">
    Search results in <U>Kitt debug UART</U> for : " <mark>{{ q }}</mark> " 
</div>

<div id="searchterm" class="search_results">
    {{ res }}
</div>

The checkbox is always marked as unchecked !该复选框始终标记为未选中! Please provide me with any possible solutions !请为我提供任何可能的解决方案!

Thanks!谢谢!

Missed a key thing from your POST.错过了您的 POST 中的关键内容。 Your checkbox is outside of the form!您的复选框在表格之外!

Submitting the form will always submit a value for checkbox.提交表单将始终为复选框提交一个值。

When it is checked, the value will be 'on' .选中后,该值将为'on' When it is unchecked, it will be an empty string ie ""当它被取消选中时,它将是一个空字符串,即""

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

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