我需要顯示一些選擇選項。一旦選擇了類別,我想用另一個選擇顯示子類別。
我的flask代碼
def return_panim():
panim = ["", "????? ????? ????", "????? ????", "????? ?????? ????", "????? ????? ????? ??????", "????? ???? ????'??? ????????", "????? ???? ????'??? ????? ????"]
data = """
<label>???????</label>
<select id="subCategory" onchange="loadDoc()">"""
for pan in panim:
data = data + '<option value=' + '"' + pan + '"' + ">" + '"' + pan + '"' + '</option>'
data = data + '</select>'
return data
def return_categories():
category_list = ["", "????? ????", "????? ???? ????", "????? ???? ?????????", "????? ????", "??????", "??????", "?????", "????? ?????"]
data = """
<label>?? ???????</label>
<select id="category" onchange="loadDoc()">"""
for category in category_list:
data = data + '<option value=' + '"' + category + '"' + '>' + category + '</option>'
data = data + "</select>"
return data
@app.route("/appoint", methods=["GET", "POST"])
def apple(name=None):
if request.method == "POST":
r = request.get_data().decode("utf-8")
print(r)
if r == "????? ????":
print(return_panim())
return render_template("appoint.html", data_category=return_categories(), data_subcategory=return_panim())
return render_template("appoint.html", data_category=return_categories())
我的客戶端
<html>
<head>
</head>
<body>
{{ data_category | safe }}
{{ data_subcategory | safe }}
<script>
function loadDoc() {
let encoder = new TextEncoder();
const xhttp = new XMLHttpRequest();
var sel = document.getElementById("category");
var text = sel.value;
xhttp.open("POST", "https://www.annastudio.beauty/appoint", true);
xhttp.send(encoder.encode(text));
}
</script>
</body>
</html>
當我試圖無條件返回時
@app.route("/appoint", methods=["GET", "POST"])
def apple(name=None):
return render_template("appoint.html", data_category=return_categories(), data_subcategory=return_panim())
它將渲染兩個選擇。
我認為您不必為此任務(wù)使用Ajax。
以下示例使用兩個字段集。根據(jù)在第一個select元素中所做的選擇,使用JavaScript啟用或禁用第二個字段集。禁用的字段集使用CSS隱藏。
如果不想使用字段集,可以以相同的方式禁用和啟用各個輸入字段。