[英]Is there a way, to change an action of a form two selections (two selects) after a button press?
我正在為我的IoT系統設置一個網頁,以便能夠創建If This Then That語句。 我希望能夠從兩個選擇中進行選擇。 然后,按下按鈕后,我希望被重定向到另一個頁面/ {{{首選值}} / {{第二選擇值}} /。
我讓它適用於一個選擇,當第一個選擇被更改並且更改表單操作時,會調用一個js函數。 我不知道如何更改代碼以使用第二個選擇來執行此操作,因為我不是真的流利的js。
Html模板(if_create_start.html):
<form action="" method = "POST" id = "FORM_ID">
<p>
<select name="list_sensors_select" required onchange="changeAction(this)">
<option disabeled selected>Choose a Sensor</option>
{% for key, value in list_sensors.items() %}
<option value="{{ key }}">{{ value["nice_name"] }}</option>
{% endfor %}
</select>
<script>
changeAction = function(select){
document.getElementById("FORM_ID").action = select.value;
}
</script>
</p>
<p>
<select name="until_select">
<option selected value="none">Function Disabled</option>
<option value="time">Amount of Time Passed</option>
<option value="button">Button Pressed</option>
</select>
</p>
<p><input type="submit" value="Submit" /></p>
</form>
webapp.py:
@app.route('/if/', methods = ['POST', 'GET'])
def if_dash():
with open('sensors.json') as json_file:
list_sensors = json.load(json_file)
return render_template("if_create_start.html", list_sensors=list_sensors)
@app.route('/if/<real_name>', methods = ['POST'])
def if_sensor(real_name):
with open('sensors.json') as json_file:
list_sensors = json.load(json_file)
if not real_name in list_sensors:
abort(404)
else:
if request.method == 'POST':
result = request.form
return render_template("result.html", result=result)
json文件:
{
"garage_door": {"nice_name": "Garage Door Sensor", "type": "float"},
"sensor1": {"nice_name": "Sensor 1", "type": "float"},
"sensor2": {"nice_name": "Sensor 2", "type": "time"}
}
正如我所說,按下按鈕后,我想要路由到/ if / {{list_senors_select value}} / {{until_select value}} /。 我想我需要修改js腳本,但我不知道如何。 我會感謝任何幫助。
我從來不知道提供多少信息/腳本,所以如果我提供太多/太少,請隨時與我聯系,我會減少它。
謝謝!
當select1或select2更改時,您需要更改操作:
<select id="select1" name="list_sensors_select" required onchange="changeAction()">
<select id="select2" name="until_select" onChange="changeAction()">
function changeAction() {
// Sensor
var select1 = document.getElementById("select1");
var val1 = select1.options[select1.selectedIndex].value;
// Until
var select2 = document.getElementById("select2");
var val2 = select2.options[select2.selectedIndex].value;
// Change form action
document.getElementById("FORM_ID").action = "/" + val1 + "/" + val2;
}
編輯:您還需要檢查until_select
是否為非/禁用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.