【问题标题】:HTML <select> and <option> conditional tagHTML <select> 和 <option> 条件标签
【发布时间】:2018-03-15 00:31:02
【问题描述】:

我正在创建一个在线表单,允许用户将业务客户输入数据库 - 没什么复杂的。根据为第一个问题(国家)选择的答案,应出现一个随后的下拉菜单,提示用户选择省/州,然后是另一个下拉菜单,提示用户选择该省/州内的城市。

到目前为止,没有一个条件作用。第一个问题显示国家/地区选择的下拉菜单,但选择任何选项都不会提示任何其他问题。

    <script>
    
    function displayCountry(answer) {
      document.getElementById(answer).style.display = "block";
      if (answer == "China") { 
        document.getElementById("India").style.display = "none";
        document.getElementById("USA").style.display = "none"; }
      else if (answer == "India") {
        document.getElementById("China").style.display = "none";
        document.getElementById(USA).style.display = "none"; }
      else if (answer == "USA") {
        document.getElementById("China").style.display = "none";
        document.getElementById("India").style.display = "none"; }
    }
    
    function displayProvince(answer) {
      document.getElementById(answer).style.display = "block";
      if (answer == "Beijing Municipality") { 
        document.getElementById("Tianjin Municipality").style.display = "none"; }
    
      else if (answer == "Tianjin Municipality") {
        document.getElementById("Beijing Municipality").style.display = "none"; }
    }
    
    function displayChinaCity(answer) {
      document.getElementById(answer).style.display = "block";
      if (answer == "Beijing") { 
        document.getElementById("Dongcheng").style.display = "none"; }
    
      else if (answer == "Dongcheng") {
        document.getElementById("Beijing").style.display = "none"; }
    }
    
    </script>
     
     <!-- ############################################################################-->
    
    <div class="container">
        <h3>Add Client</h3>  
            <div class="tab-content">         
                <form action="/add/clients" method="post">
            <!-- ################################ Client ID ################################-->
                  <div class="top-row">
                    <div class="field-wrap">
                    <label>
                      Client ID<span class="req">*</span>
                      <input>
                    </label>
                    </div>
                  </div>
      <!-- ################################ Client name #############################-->
                  <div class="top-row">
                    <div class="field-wrap">
                    <label>
                      Client name<span class="req">*</span>
                      <input>
                    </label>
                    </div>
                  </div>
    
        <!-- ############################## Client type ##############################-->
                  <div class="field-wrap">
                    <label>
                      Client type<span class= "req">*</span>
                    <select>
                    <!--Removed for simplicity -->
                    </select></label>
                  </div>             
        <!-- ########################### Client location #############################-->
                  <div class="field-wrap">
                    <form name="feedback" action= "javascript:void(0)">
                      <label>Client Origin<span class="req">*</span>
                        <select name= "country">
                          <option selected= "--">--</option>
                          <option value= "China" onchange= "displayCountry(this.value)" value= "China">China</option>
                          <option value= "India" onchange= "displayCountry(this.value)" value= "India">India</option>
                          <option value= "USA" onchange= "displayCountry(this.value)" value= "USA">USA</option>
                        </select>
                      </label>
    
                      <div id= "China" style= "display:none;"><br/>
                        Select Province<span class="req">*</span>
                        <select name= "province">
                          <option selected= "--">--</option>
                          <option value= "Beijing Municipality" onchange= "displayProvince(this.value)" value= "Beijing Municipality">Beijing Municipality></option>
                          <option value= "Tianjin Municipality" onchange= "displayProvince(this.value)" value= "Tianjin Municipality">Tianjin Municipality></option>
                          <!--More options removed for simplicity -->
    
                        </select>
                      </div>
                      <div id= "Beijing Municipality" style= "display:none;"><br/>
                        Select City<span class="req">*</span>
                        <select name= "city">
                          <option selected= "--">--</option>
                          <option value= "Beijing" onchange= "displayChinaCity(this.value)" value= "Beijing">Beijing</option>
                          <option value= "Dongcheng" onchange= "displayChinaCity(this.value)" value= "Dongcheng">Dongcheng</option>
                          <!--More options removed for simplicity-->
                        </select>
                      </div>
                      <!--divs for other cities,provinces,and countries omitted for simplicity-->
                    </form>
                  </div> 
          </form>
        </div>
    </div>

【问题讨论】:

  • 仍然像上次一样出现语法错误(&lt;option /&gt;,缺少大括号...)
  • 我不确定你的意思。为什么使用&lt;option /&gt;
  • 某些 HTML 无效,例如:&lt;option /&gt;China&lt;/option&gt;。 JavaScript 缺少一对右括号。
  • 我更正了缺失的大括号。关于&lt;option&gt; 标签,我认为这是有效的?还是我需要使用&lt;option value="China"&gt;China&lt;/option&gt;
  • 是的,就是这样。如需参考,请参阅&lt;select&gt;

标签: javascript html


【解决方案1】:

我看到的主要问题是 onchange 处理程序应该绑定到 &lt;select&gt; 元素而不是 &lt;option&gt; 元素。

好像还有nested &lt;form&gt;,可能会出问题。

function displayCountry(answer) {
  document.getElementById(answer).style.display = "block";
  if (answer == "China") {
    document.getElementById("India").style.display = "none";
    document.getElementById("USA").style.display = "none";
  } else if (answer == "India") {
    document.getElementById("China").style.display = "none";
    document.getElementById("USA").style.display = "none";
  } else if (answer == "USA") {
    document.getElementById("China").style.display = "none";
    document.getElementById("India").style.display = "none";
  }
}

function displayProvince(answer) {
  document.getElementById(answer).style.display = "block";
  if (answer == "Beijing Municipality") {
    document.getElementById("Tianjin Municipality").style.display = "none";
  } else if (answer == "Tianjin Municipality") {
    document.getElementById("Beijing Municipality").style.display = "none";
  }
}

function displayChinaCity(answer) {
  document.getElementById(answer).style.display = "block";
  if (answer == "Beijing") {
    document.getElementById("Dongcheng").style.display = "none";
  } else if (answer == "Dongcheng") {
    document.getElementById("Beijing").style.display = "none";
  }
}
<div class="container">
  <h3>Add Client</h3>
  <div class="tab-content">
    <form action="/add/clients" method="post">

      <div class="top-row">
        <div class="field-wrap">
          <label>Client ID<span class="req">*</span><input></label>
        </div>
      </div>

      <div class="top-row">
        <div class="field-wrap">
          <label>Client name<span class="req">*</span><input></label>
        </div>
      </div>

      <div class="field-wrap">
        <label>Client type<span class= "req">*</span><select></select></label>
      </div>


      <div class="field-wrap">
      
        <label>Client Origin<span class="req">*</span>
          <select name="country" onchange="displayCountry(this.value)">
            <option selected= "--">--</option>
            <option value= "China" >China</option>
            <option value= "India" >India</option>
            <option value= "USA"  >USA</option>
          </select>
        </label>

        <div id="USA" style="display:none;">
          <select></select>
        </div>

        <div id="China" style="display:none;"><br/>
          Select Province<span class="req">*</span>
          <select name="province" onchange="displayProvince(this.value)">
            <option selected= "--">--</option>
            <option value= "Beijing Municipality" >Beijing Municipality</option>
            <option value= "Tianjin Municipality">Tianjin Municipality</option>
          </select>
        </div>
        
        <div id="India" style="display:none;">
          <select></select>
        </div>

        <div id="Beijing Municipality" style="display:none;"><br/>
          Select City<span class="req">*</span>
          <select name="city" onchange="displayChinaCity(this.value)">
            <option selected= "--">--</option>
            <option value= "Beijing">Beijing</option>
            <option value= "Dongcheng">Dongcheng</option>
          </select>
        </div>

      </div>
    </form>
  </div>
</div>

【讨论】:

  • 您,先生,是个天才。谢谢!得到完整版本的代码来工作
  • 一个后续问题:如果我想用所选城市的相应邮政编码自动填充后续表单字段,我该怎么做?我会在函数中定义它还是只在html中编码?非常感谢
  • 这可能最好作为一个单独的问题提出。您如何确定任何特定城市的邮政编码?
  • 我刚刚意识到每个城市可能有多个邮政编码。可能不得不考虑一下。如果需要,我将发布一个单独的问题。感谢您的帮助
  • 哦,我想一个简单的替代方法是根据位置要求邮政编码有一定数量的数字,即美国的邮政编码包含 5 位数字,中国的邮政编码包含 6 位数字。 .. 但是,我不确定如何在用户选择城市后(而不是在他们选择国家后)放置此表单域
猜你喜欢
  • 2011-08-11
  • 1970-01-01
  • 2016-07-14
  • 2013-10-22
  • 2022-09-23
  • 1970-01-01
  • 2011-01-13
  • 1970-01-01
  • 2010-12-21
相关资源
最近更新 更多