使用Django作为后端在同一个HTML页面中将所选选项从一种形式传递到另一种形式

时间:2018-11-14 23:50:11

标签: javascript html django

我有两个表格。我想将选择的选项从第一种形式传递给另一种形式作为隐藏输入。我怎样才能做到这一点。我尝试了以下解决方案,但没有用。

<form id="headerform" method="GET" action="{% url 'some url' %}">
    {% if all_p_cities %}
        <select id='pre_sel_city' name="h_qc"  onchange="headerform.submit()">
            {% for city in all_p_cities %}
            {% if city.city_name == post_city %}
            <option value="{{city.city_name}}" selected > {{post_city}}
            </option>

            {% else %}
            <option value="{{city.city_name}}"> {{city.city_name}}
            </option> 

            {% endif %}
            {% endfor %}
        </select>
    {% else %}
        <select id='pre_sel_city' name="h_qc"  onchange="headerform.submit()">

            <option value="{{post_city}}" selected > {{post_city}}
            </option>

        </select>
    {% endif %}
</form>

第二种形式如下:

<form id='query-box'>
    <!-- predetermined search fields -->
    <input id='sel_city' type="hidden" name="h_qc" value=''>
    <input type="text">
    <button type="submit">
</form>

我使用javascript将第一种形式的值传递给另一种形式:

<script type="text/javascript">
    //get the input elements from HTML DOM for preselected input from the main search to customized header search

    var sel_city = document.getElementById("sel_city");

    var pre_sel_city = document.getElementById("pre_sel_city");

    //Get the value of inputs from first form 
    var pre_sel_city_value = pre_sel_city.value;

    //Assign the values of first form to the second
    pre_sel_city.value = pre_sel_city_value;

</script>

1 个答案:

答案 0 :(得分:1)

由于我正在使用手机,因此无法对其进行测试,因此我会将其发布为评论。 你有     pre_sel_city.value = pre_sel_city_value;

尝试:

//Assign the values of first form to the second
sel_city.value = pre_sel_city_value;