有没有办法,在按下按钮后改变表格两个选项(两个选择)的动作?

问题描述 投票:0回答:1

我正在为我的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>

Web app.朋友:

@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脚本,但我不知道如何。我会感谢任何帮助。

我不知道要提供多少信息/脚本,所以如果我提供的太多/太少,请随时与我联系,我会减少它。

谢谢!

javascript html html-form
1个回答
0
投票

当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是否不是/禁用。

© www.soinside.com 2019 - 2024. All rights reserved.