簡體   English   中英

選中單選按鈕的重定向值顯示在

[英]Redirect value from checked radio button shows on

我有一個帶有相應單選按鈕的無序列表。 選擇單選按鈕並按下提交按鈕后,我想獲取該值並在重定向的 url 中使用它。 表單提交成功,但是,它返回的值是“on”/operations/on 而不是選擇的實際值。 我已經嘗試過使用常規 method=POST 以及 ajax 來返回該值,但沒有運氣。

HTML:

<form id="warehouse-selection-form" action="/" method="post">
    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
    <div class="tab-content mb-4" id="myTabContent">
        {% for key, values in warehouses.items() %}
            <div class="tab-pane fade" id="{{ key.lower().replace('-', '').replace('  ', ' ').replace(' ', '-') }}" role="tabpanel" aria-labelledby="{{ key.lower().replace('-', '').replace('  ', ' ').replace(' ', '-') }}-tab">
            {% for value in values %}
                <label class="btn btn-outline-secondary col-1 text-center rounded px-3 m-1" for="{{ value }}">{{ value }}</label>
                <input type="radio" class="btn-check" name="radio" id="{{ value }}" autocomplete="off">
            {% endfor %}
            </div>
        {% endfor %}
    </div>

    <button id="submit" style="display:none;" class="btn btn-primary" onclick="submitWarehouse()">Submit</button>
</form>

燒瓶:

@select_warehouse_bp.route("/", methods=["GET", "POST"])
def get_home():
    warehouse = request.form['radio']
    return redirect(url_for('select_warehouse_bp.get_operations', warehouse=warehouse))


@select_warehouse_bp.route("/operations/<warehouse>", methods=["GET", "POST"])
def get_buildings(warehouse):
    return render_template("base.html")

阿賈克斯:

  • 如果使用此方法和 method=post 刪除 CSRF 令牌
  • 我還嘗試使用 location.href 重定向以避免傳回服務器,但沒有運氣。
function submitWarehouse(){
    let warehouse = $('input[name="radio"]:checked').attr('id');

    $.ajax({
        url: `/operations/${warehouse}`,
        type: "POST",
        data: JSON.stringify({warehouse: warehouse}),
        dataType: 'json',
         success: function () {
            alert('success');
        },
        error: function () {
            alert('fail');
        }
    });
}

您沒有為 input 單選標簽使用 value 屬性,如果您沒有為 HTML 單選輸入設置值,它將默認返回“on”值;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM