我有一個帶有相應單選按鈕的無序串列。選擇單選按鈕并按下提交按鈕后,我想獲取該值并在重定向的 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');
}
});
}
uj5u.com熱心網友回復:
您沒有為輸入單選標簽使用 value 屬性,如果您沒有為 HTML 單選輸入設定值,它將默認回傳“on”值;
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/334958.html
標籤:javascript html 阿贾克斯 烧瓶
上一篇:將資料輸入表單欄位的Javascript錯誤自定義函式
下一篇:標題和側邊欄之間的間距問題
