使用jQuery过滤后续下拉列表

时间:2013-07-22 15:02:28

标签: jquery html forms

您好,感谢您的阅读。我有3个下拉菜单,1个用于过滤,2个用于提交值。我需要做的是让第一个下拉菜单过滤第三个值,以便显示的唯一值是与第一个值匹配的值。我试图根据他们的课程过滤他们,但我遇到了麻烦。按ID过滤会更容易吗?

以下是相关表格:

<form class="form-horizontal well" method="post" action="/cgi-bin/dropdown.py">
    <fieldset>
      <legend>Select Audit Point</legend>

      <div class="control-group">
        <label class="control-label" for="select01">Select Tier</label>
        <div class="controls">
          <select id="select01" name="select01">
            <option value="All Tiers">All Tiers</option>
            <option value="Database Tier">Database Tier</option>
            <option value="Application Tier">Application Tier</option>
            <option value="Web Tier">Web Tier</option>
          </select>
        </div>
      </div>

      <div class="control-group">
        <label class="control-label" for="select01">Select Version</label>
        <div class="controls">
          <select id="select02" name="select02">
            <option value="Oracle" name="Oracle">Oracle</option>
            <option value="SQL" name="SQL">SQL</option>
            <option value="Both" name="Both">Both</option>
          </select>
        </div>
      </div>

      <div class="control-group">
        <label class="control-label" for="select01">Select Audit Point</label>
        <div class="controls">
          <select id="select03" name="select03">
            <option class="All Tiers" value="Eagle Software Version">Eagle Software Version</option>
            <option class="All Tiers" value="Server OS, Memory, CPU">Server OS, Memory, CPU</option>
            <option class="All Tiers" value="High Availability">High Availability</option>
            <option class="Database Tier" value="DBMS Version">DBMS Version</option>
            <option class="Database Tier" value="DBMS Statistics">DBMS Statistics</option>
            <option class="Database Tier" value="DBMS Parameters">DBMS Parameters</option>
            <option class="Database Tier" value="DBMS Data File Growth">DBMS Data File Growth</option>
            <option class="Database Tier" value="Database Disk Structure">Database Disk Structure</option>
            <option class="Database Tier" value="Long Running SQL">Long Running SQL</option>
            <option class="Database Tier" value="Security Data Growth Range">Security Data Growth Range</option>
            <option class="Database Tier" value="Extraneous entries in System Tables">Extraneous entries in System Tables</option>
            <option class="Database Tier" value="Feed Error Orphans">Feed Error Orphans</option>
            <option class="Database Tier" value="Position Detail Orphans">Position Detail Orphans</option>
            <option class="Database Tier" value="Data Retention Policy">Data Retention Policy</option>
            <option class="Database Tier" value="Securities Added Daily">Securities Added Daily</option>
            <option class="Database Tier" value="Security Master Load">Security Master Load</option>
            <option class="Database Tier" value="SRM Purge">SRM Purge</option>
            <option class="Database Tier" value="Best Pricing Purge">Best Pricing Purge</option>
            <option class="Database Tier" value="Partitioning">Partitioning</option>
            <option class="Database Tier" value="DBMS Data/Log File Utilize Shared Disk">DBMS Data/Log File Utilize Shared Disk</option>
            <option class="Database Tier" value="DBMS Failover">DBMS Failover</option>
            <option class="Application Tier" value="DBMS Client Version">DBMS Client Version</option>
            <option class="Application Tier" value="Star Engine IP Configuration">Star Engine IP Configuration</option>
            <option class="Application Tier" value="Engine Count/Configuration">Engine Count/Configuration</option>
            <option class="Application Tier" value="STAR Engine Logging Interval">STAR Engine Logging Interval</option>
            <option class="Application Tier" value="PACE Engine Port Configuration">PACE Engine Port Configuration</option>
            <option class="Application Tier" value="PACE Server Logging">PACE Server Logging</option>
            <option class="Application Tier" value="PACE Engine Log Configuration">PACE Engine Log Configuration</option>
            <option class="Application Tier" value="STAR Engine Load Balancer Interval">STAR Engine Load Balancer Interval</option>
            <option class="Application Tier" value="Engines Restarted Weekly">Engines Restarted Weekly</option>
            <option class="Application Tier" value="Designated Master">Designated Master</option>
            <option class="Application Tier" value="Cluster Managers Identical">Cluster Managers Identical</option>
            <option class="Application Tier" value="Uploader Shared Disk Space">Uploader Shared Disk Space</option>
            <option class="Application Tier" value="Custom Archive Rule Shared Disk Space">Custom Archive Rule Shared Disk Space</option>
            <option class="Application Tier" value="App Server Clustering">App Server Clustering</option>
            <option class="Application Tier" value="PACE Event Concurrency">PACE Event Concurrency</option>
            <option class="Application Tier" value="Homogenous Engine Configuration">Homogenous Engine Configuration</option>
            <option class="Web Tier" value="Log Levels">Log Levels</option>
            <option class="Web Tier" value="Scheduler Purging">Scheduler Purging</option>
            <option class="Web Tier" value="Web Server Services Restarted Weekly">Web Server Services Restarted Weekly</option>
            <option class="Web Tier" value="Email Notification Basic Configuration">Email Notification Basic Configuration</option>
            <option class="Web Tier" value="Eagle Web Load Balancer Configuration"n>Eagle Web Load Balancer Configuration</option>
            <option class="Web Tier" value="Load Balancer Customizations">Load Balancer Customizations</option>
            <option class="Web Tier" value="Portal Shared Disk Space">Portal Shared Disk Space</option>
            <option class="Web Tier" value="Message Center Shared Disk Space">Message Center Shared Disk Space</option>
            <option class="Web Tier" value="Message Center ID'S">Message Center ID'S</option>
            <option class="Web Tier" value="Schedule Service Config">Schedule Service Config</option>
            <option class="Web Tier" value="ePace is a client of Clustered App Servers">ePace is a client of Clustered App Servers</option>
            <option class="Web Tier" value="Portal is a client of Clustered App Servers">Portal is a client of Clustered App Servers</option>
            <option class="Web Tier" value="ESTAR is a client of Clustered Load Balancers">ESTAR is a client of Clustered Load Balancers</option>
            <option class="Web Tier" value="ESTAR is a client of Clustered Engines">ESTAR is a client of Clustered Engines</option>
            <option class="Web Tier" value="ESTAR is a client of Clustered Report Export Services">ESTAR is a client of Clustered Report Export Services</option>
            <option class="Web Tier" value="WebSync Configured">WebSync Configured</option>
            <option class="Web Tier" value="Web Server Load Balancing Configured">Web Server Load Balancing Configured</option>
            <option class="Web Tier" value="Shared Disk Dynamic Folder">Shared Disk Dynamic Folder</option>

          </select>
        </div>
      </div>


      <div class="form-actions">
        <button type="submit" class="btn btn-primary" value="Submit">Submit</button>
        <button type="reset" class="btn">Cancel</button>
      </div>
    </fieldset>
  </form>

这是我以前用过的jQuery。有了它,我成功地按其值过滤了第三个列表,但在遇到一些POST问题后,我不得不更改值并且卡住了。

   <script type="text/javascript">
   $(document).ready(function() {

        var savedOptions = '';

        savedOptions = $('#select03').html(); //save the second dropdown-list

        $('#select01').change(function() {
            var selectedValue = $('#select01').val(); //After changing the value of the first dropdown, store this value inside a variable

           $('#select03').html(savedOptions); //restore the content of the 2nd dropdown
            $('#select03').children('option').each(function() {
                if($(this).attr('value') != selectedValue) {
                    $(this).remove(); //Compare and step through the 2nd dropdown and delete all unneccessary options
                }
            });
        });
    });
    </script>

1 个答案:

答案 0 :(得分:1)

您正在检查该值,而不是该选项是否具有该类。您希望if语句为:

if(!$(this).hasClass(selectedValue)) {

您正在检查每个选项的值,以查看它是否与该类匹配。当然它没有,这就是为什么你没有过滤任何东西。

您可以在此处看到它:http://jsfiddle.net/UqaqS/1/

此外,您实际上不需要在过滤下拉列表中包含两个单词,只需使用AllDatabaseWeb而不是Web Tier {1}}等等你可以按第一个单词过滤。您只是在所有选项中添加第二个类,这似乎并不重要。