显示基于下拉菜单的表单:JavaScript 和 HTML

huangapple go评论87阅读模式
英文:

Show forms based on dropdown menu: javascript and html

问题

你有这个HTML,我想根据从下拉菜单中选择的值来显示不同的表单。根据这个答案,我提出了以下解决方案,但它并不如我所愿,也就是说,无论选择什么,所有表单都从一开始就显示出来。

我还尝试了JavaScript函数的其他版本,但没有太大的成功。这个HTML应该通过Flask函数呈现。感谢任何建议。

英文:

I have this html, in which I want to show different forms, based on a value chosen from a dropdown menu. Base on this answer I came up with this solution, which does not do what I intend, meaning all the form are shown from the beginning, no matter the choice.

  1. {% extends "base.html" %}
  2. {% block title %}Information {{configuration}} {% endblock %}
  3. {% block content %}
  4. <div class="px-2 py-5">
  5. <h3> info {{configuration}}</h3>
  6. <p>Here you can input the necessary informations </p>
  7. <form action="/new-simulation" method='POST'>
  8. <div class="mb-3">
  9. <div class="mb-3">
  10. <label for="info_type" class="form-label">info Type</label>
  11. <select class="form-select" id="info_type" name="info_type">
  12. <option value="general">General Inquiry</option>
  13. <option value="credit">Credit Inquiry</option>
  14. <option value="payment">Payment Issue</option>
  15. </select>
  16. </div>
  17. <div class="general" id="general">
  18. <label for="select">How did you find out about us?<span>*</span></label><br>
  19. <select name="case" id="case-type">
  20. <option value="value1">Value 1</option>
  21. </select><br>
  22. </div>
  23. <div class="credit" id="credit">
  24. <label for="Date of Inquiry">Date of Inquiry<span>*</span></label>
  25. <input type="date">
  26. <label for="Agency">Agency 3 <span>*</span></label>
  27. <input type="text">
  28. </div>
  29. <div class="payment" id="payment">
  30. <label for="Service Phone Number">Service Phone Number<span>*</span></label>
  31. <input type="text">
  32. <label for="select">Topic<span>*</span></label><br>
  33. <select name="case" id="case-type">
  34. <option value="topic1">Topic 1</option>
  35. </select><br><br>
  36. </div>
  37. <script type="text/javascript">
  38. // hide all the divs
  39. $('div').hide()
  40. // Show and hide selected div
  41. $('#info_type').change(function () {
  42. var value = this.value;
  43. $('div').hide()
  44. $('#' + this.value).show();
  45. });
  46. </script>
  47. <div class="row mb-3">
  48. <div class="col">
  49. <button style="margin:5px;" class="btn btn-secondary" type="submit">submit</button>
  50. </div>
  51. </div>
  52. </form>
  53. </div>
  54. {% endblock %}

I also tried other version of the javascript function, without much of a success. This html is supposed to be rendered through flask function.
Any suggestion is appreciated.

答案1

得分: 0

将它们包装在一个容器中,隐藏它,然后切换每个,除了选定的一个。

尝试这个:

  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  2. <form action="/new-simulation" method='POST'>
  3. <div class="mb-3">
  4. <div class="mb-3">
  5. <label for="info_type" class="form-label">信息类型</label>
  6. <select class="form-select" id="info_type" name="info_type">
  7. <option value="choose">--选择--</option>
  8. <option value="general">一般查询</option>
  9. <option value="credit">信用查询</option>
  10. <option value="payment">付款问题</option>
  11. </select>
  12. </div>
  13. <div class="hidden">
  14. <div class="general" id="general">
  15. <label for="select">您是如何得知我们的?<span>*</span></label>
  16. <br>
  17. <select name="case" id="case-type">
  18. <option value="value1">选项 1</option>
  19. </select>
  20. <br>
  21. </div>
  22. <div class="credit" id="credit">
  23. <label for="Date of Inquiry">查询日期<span>*</span></label>
  24. <input type="date">
  25. <label for="Agency">机构 3<span>*</span></label>
  26. <input type="text">
  27. </div>
  28. <div class="payment" id="payment">
  29. <label for="Service Phone Number">服务电话号码<span>*</span></label>
  30. <input type="text">
  31. <label for="select">主题<span>*</span></label>
  32. <br>
  33. <select name="case" id="case-type">
  34. <option value="topic1">主题 1</option>
  35. </select>
  36. <br>
  37. <br>
  38. </div>
  39. </div>
  40. <div class="row mb-3">
  41. <div class="col">
  42. <button style="margin:5px;" class="btn btn-secondary" type="submit">提交</button>
  43. </div>
  44. </div>
  45. </div>
  46. </form>
英文:

Wrap them in a container, hide it, and then toggle each except the selected one.

Try this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. $(&quot;#info_type&quot;).change(function(){
  2. var value = $(this).val();
  3. if(value == &#39;choose&#39;) return;
  4. $(&quot;#&quot; + value).toggle().siblings().hide();
  5. });

<!-- language: lang-css -->

  1. .hidden div {
  2. display: none;
  3. }

<!-- language: lang-html -->

  1. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
  2. &lt;form action=&quot;/new-simulation&quot; method=&#39;POST&#39;&gt;
  3. &lt;div class=&quot;mb-3&quot;&gt;
  4. &lt;div class=&quot;mb-3&quot;&gt;
  5. &lt;label for=&quot;info_type&quot; class=&quot;form-label&quot;&gt;info Type&lt;/label&gt;
  6. &lt;select class=&quot;form-select&quot; id=&quot;info_type&quot; name=&quot;info_type&quot;&gt;
  7. &lt;option value=&quot;choose&quot;&gt;--Choose--&lt;/option&gt;
  8. &lt;option value=&quot;general&quot;&gt;General Inquiry&lt;/option&gt;
  9. &lt;option value=&quot;credit&quot;&gt;Credit Inquiry&lt;/option&gt;
  10. &lt;option value=&quot;payment&quot;&gt;Payment Issue&lt;/option&gt;
  11. &lt;/select&gt;
  12. &lt;/div&gt;
  13. &lt;div class=&quot;hidden&quot;&gt;
  14. &lt;div class=&quot;general&quot; id=&quot;general&quot;&gt;
  15. &lt;label for=&quot;select&quot;&gt;How did you find out about us?&lt;span&gt;*&lt;/span&gt;&lt;/label&gt;
  16. &lt;br&gt;
  17. &lt;select name=&quot;case&quot; id=&quot;case-type&quot;&gt;
  18. &lt;option value=&quot;value1&quot;&gt;Value 1&lt;/option&gt;
  19. &lt;/select&gt;
  20. &lt;br&gt;
  21. &lt;/div&gt;
  22. &lt;div class=&quot;credit&quot; id=&quot;credit&quot;&gt;
  23. &lt;label for=&quot;Date of Inquiry&quot;&gt;Date of Inquiry&lt;span&gt;*&lt;/span&gt;&lt;/label&gt;
  24. &lt;input type=&quot;date&quot;&gt;
  25. &lt;label for=&quot;Agency&quot;&gt;Agency 3 &lt;span&gt;*&lt;/span&gt;&lt;/label&gt;
  26. &lt;input type=&quot;text&quot;&gt;
  27. &lt;/div&gt;
  28. &lt;div class=&quot;payment&quot; id=&quot;payment&quot;&gt;
  29. &lt;label for=&quot;Service Phone Number&quot;&gt;Service Phone Number&lt;span&gt;*&lt;/span&gt;&lt;/label&gt;
  30. &lt;input type=&quot;text&quot;&gt;
  31. &lt;label for=&quot;select&quot;&gt;Topic&lt;span&gt;*&lt;/span&gt;&lt;/label&gt;
  32. &lt;br&gt;
  33. &lt;select name=&quot;case&quot; id=&quot;case-type&quot;&gt;
  34. &lt;option value=&quot;topic1&quot;&gt;Topic 1&lt;/option&gt;
  35. &lt;/select&gt;
  36. &lt;br&gt;
  37. &lt;br&gt;
  38. &lt;/div&gt;
  39. &lt;/div&gt;
  40. &lt;div class=&quot;row mb-3&quot;&gt;
  41. &lt;div class=&quot;col&quot;&gt;
  42. &lt;button style=&quot;margin:5px;&quot; class=&quot;btn btn-secondary&quot; type=&quot;submit&quot;&gt;submit&lt;/button&gt;
  43. &lt;/div&gt;
  44. &lt;/div&gt;
  45. &lt;/div&gt;
  46. &lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月27日 18:10:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定