【问题标题】:Only show list option based on earlier form choice仅显示基于早期表单选择的列表选项
【发布时间】:2015-08-10 10:22:09
【问题描述】:

这是我的表格的一部分:

<label for="User">Select your account type:</label>
    <select name="usertype" for="usertype" id="usertype" required>
        <option value="1">Employee</option>
        <option value="2">Manager</option>
    </select>
<label for="User">Select the company you work for:</label>
    <select name="company" for="company" id="company" required>
        <?php
            include ('processors/generate-company-list.php');
        ?>
        <option value="NULL">I'll create my own company later</option>";
    </select>

我只希望&lt;option value="NULL"&gt;I'll create my own company later&lt;/option&gt; 在表单中较早选择成为经理时可用。这可能吗?

【问题讨论】:

  • 简单地使用&lt;option value="0"&gt;I'll create my own company later&lt;/option&gt;";代替null
  • 使用 value="" 而不是 value="NULL" 瞧!
  • 我正在使用 null,因为它进入了一个 SQL 数据库,在该数据库中我需要 NULL 用于稍后的代码。编辑,更改为 value=""。
  • 使用 jquery 或 javascript
  • 我不确定我是否解释清楚,我只想在他们选择经理作为他们的帐户类型时显示该选项。

标签: php html list


【解决方案1】:
    <script type="text/javascript">
    function check(){
      var usertype=document.getElementById("usertype").value;
      if(usertype==2){
          document.getElementById("company").selectedIndex = "0";
        document.getElementById("company").disabled=true;

      }else{
        document.getElementById("company").disabled=false;

      }
    }
    </script>


        <label for="User">Select your account type:</label>
        <select name="usertype" for="usertype" id="usertype" required onchange='check();'>
            <option value="1">Employee</option>
            <option value="2">Manager</option>
        </select>
    <label for="User">Select the company you work for:</label>
        <select name="company" for="company" id="company" required>
            <?php
                include ('processors/generate-company-list.php');
            ?>
<option value="NULL">I'll create my own company later</option>";
              <option value="1"> listitem1 </option>";
              <option value="1"> listitem2</option>";


        </select>

我使用JavaScript函数选择0时选择0索引并禁用第二选择框。

【讨论】:

  • 这个可行,但是如果你选择经理,然后选择创建自己的公司,然后选择员工,创建自己的公司仍然存在。
猜你喜欢
  • 2015-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 2014-06-20
  • 2012-03-27
相关资源
最近更新 更多