代码之家  ›  专栏  ›  技术社区  ›  Black

jQueryUI selectmenu-设置选定选项

  •  0
  • Black  · 技术社区  · 6 年前

    我试着将下面截图中的第二个下拉列表的值设置为“Prof”,然后禁用它。

    enter image description here

    我这样试过:

    $salutation.find('option').filter(function() {
        return ($(this).text().includes('Prof'));
    }).prop('selected', true);
    

    就像这样:

    $salutation.find('option').each(function() {
        var optionText = $(this).text();
        if (optionText.includes('Prof'))
            $(this).prop('selected', true);
    });
    

    完整示例:

    $( function() {
    
      $( "#speed" ).selectmenu();
      $( "#salutation" ).selectmenu();
        
      $speed = $('#speed');
      $salutation = $('#salutation');
      test();
      
    
      $speed.on('selectmenuchange', function() {
    	    test();
      });
    
      
      function test()
      {
          let selectedOption = $speed.find('option:selected').text().trim().toLowerCase();
    
          if ( ! selectedOption.includes('fast')) {
            
            $salutation.find('option').filter(function() {
                return ($(this).text().includes('Prof'));
            }).prop('selected', true);
            
            $salutation.selectmenu('disable');
            $salutation.selectmenu('refresh');
            
          } else {
            $salutation.selectmenu('enable');
          }
      }
      
    });
    fieldset {
      border: 0;
    }
    label {
      display: block;
      margin: 30px 0 0 0;
    }
    .overflow {
      height: 200px;
    }
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    
    
     
    <div class="demo">
     
    <form action="#">
     
      <fieldset>
        <label for="speed">Select a speed</label>
        <select name="speed" id="speed">
          <option>Slower</option>
          <option>Slow</option>
          <option selected="selected">Medium</option>
          <option>Fast</option>
          <option>Faster</option>
        </select>
      
        <label for="salutation">Select a title</label>
        <select name="salutation" id="salutation">
          <option disabled selected>Please pick one</option>
          <option>Mr.</option>
          <option>Mrs.</option>
          <option>Dr.</option>
          <option>Prof.</option>
          <option>Other</option>
        </select>
      </fieldset>
    </form>
    </div>
    1 回复  |  直到 6 年前
        1
  •  0
  •   Black    6 年前

    我找到了解决办法。我不得不打电话 $salutation.selectmenu('refresh'); 之后 $salutation.selectmenu('disable');

    http://api.jqueryui.com/selectmenu/#method-refresh

    $( function() {
    
      $( "#speed" ).selectmenu();
      $( "#salutation" ).selectmenu();
        
      $speed = $('#speed');
      $salutation = $('#salutation');
      test();
      
    
      $speed.on('selectmenuchange', function() {
    	    test();
      });
    
      
      function test()
      {
          let selectedOption = $speed.find('option:selected').text().trim().toLowerCase();
    
          if ( ! selectedOption.includes('fast')) {
            
            $salutation.find('option').filter(function() {
                return ($(this).text().includes('Prof'));
            }).prop('selected', true);
            
            $salutation.selectmenu('disable');
            $salutation.selectmenu('refresh');
            
          } else {
            $salutation.selectmenu('enable');
          }
      }
      
    });
    fieldset {
      border: 0;
    }
    label {
      display: block;
      margin: 30px 0 0 0;
    }
    .overflow {
      height: 200px;
    }
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    
    
     
    <div class="demo">
     
    <form action="#">
     
      <fieldset>
        <label for="speed">Select a speed</label>
        <select name="speed" id="speed">
          <option>Slower</option>
          <option>Slow</option>
          <option selected="selected">Medium</option>
          <option>Fast</option>
          <option>Faster</option>
        </select>
      
        <label for="salutation">Select a title</label>
        <select name="salutation" id="salutation">
          <option disabled selected>Please pick one</option>
          <option>Mr.</option>
          <option>Mrs.</option>
          <option>Dr.</option>
          <option>Prof.</option>
          <option>Other</option>
        </select>
      </fieldset>
    </form>
    </div>
    推荐文章