我有两个表格。我想将选择的选项从第一种形式传递给另一种形式作为隐藏输入。我怎样才能做到这一点。我尝试了以下解决方案,但是没有用。
<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>
最佳答案
由于我在手机上并且无法测试,因此我会将其作为评论发布。
你有
pre_sel_city.value = pre_sel_city_value;
尝试:
//Assign the values of first form to the second
sel_city.value = pre_sel_city_value;
关于javascript - 使用Django作为后端在同一个HTML页面中将所选选项从一种形式传递到另一种形式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53310483/