gpt4 book ai didi

javascript - 使用 Django 作为后端,将所选选项从一个表单传递到同一 html 页面中的另一个表单

转载 作者:行者123 更新时间:2023-12-03 00:44:18 25 4
gpt4 key购买 nike

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

<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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com