如何启用/禁用jQuery UI组合框?

时间:2010-10-21 16:03:17

标签: jquery combobox

我正在使用jquery UI。我有3个组合框,如果前一个字段已填满,我需要启用每个字段。

以下是我目前的代码:

 jQuery(document).ready(function(){  
        $("#box1").combobox();  
        $("#box2").combobox();  
        $("#box3").combobox();  
 }); 

1 个答案:

答案 0 :(得分:6)

您可以在启动组合框时为“已选择”设置事件侦听器,在第二个和第三个上设置已禁用的默认“状态”。一种简单的方法是将选择列表包装在具有相似id的span或div中,以便您可以定位由jQuery UI创建的生成的INPUTBUTTON项。

您没有提供太多上下文/代码,所以我创建了一个可以帮助您入门的工作示例HERE

JS:

      // init autocomplete combobox 1 with event handler
      $("#box1").combobox({
         selected: function(event, ui) {
            // now that we have an event listener, we can do whatever we like on the event.
            $("#test2 input").removeAttr('disabled');
            $("#test2 button").removeAttr('disabled');
         }
      });

      // init autocomplete combobox 2 with event handler
      $("#box2").combobox({
         selected: function(event, ui) {
             // now that we have an event listener, we can do whatever we like on the event.
            $("#test3 input").removeAttr('disabled');
            $("#test3 button").removeAttr('disabled');
         }
      });    

    $("#box3").combobox(); // init autocomplete combobox 3

    // set initial state of generated combobox 2
    $("#test2 input").attr('disabled',true);
    $("#test2 button").attr('disabled',true);  

    // set initial state of generated combobox 3
    $("#test3 input").attr('disabled',true);
    $("#test3 button").attr('disabled',true);  

HTML:

<div class="demo">
  <div class="ui-widget">
    <div id="test1">
      <label>Box 1: </label>
      <select id="box1">
        <option value="">Select one...</option>
        <option value="ActionScript">ActionScript</option>
        <option value="AppleScript">AppleScript</option>
        <option value="Asp">Asp</option>
        <option value="BASIC">BASIC</option>
        <option value="C">C</option>
        <option value="C++">C++</option>
        <option value="Clojure">Clojure</option>
        <option value="COBOL">COBOL</option>
        <option value="ColdFusion">ColdFusion</option>
        <option value="Erlang">Erlang</option>
        <option value="Fortran">Fortran</option>
        <option value="Groovy">Groovy</option>
        <option value="Haskell">Haskell</option>
      </select>
    </div>
    <div id="test2">
      <label>Box 2: </label>
      <select id="box2">
        <option value="">Select one...</option>
        <option value="Java">Java</option>
        <option value="JavaScript">JavaScript</option>
        <option value="Lisp">Lisp</option>
        <option value="Perl">Perl</option>
        <option value="PHP">PHP</option>
        <option value="Python">Python</option>
        <option value="Ruby">Ruby</option>
        <option value="Scala">Scala</option>
        <option value="Scheme">Scheme</option>
      </select>
    </div>
    <div id="test3">
      <label>Box 3: </label>
      <select id="box3">
        <option value="">Select one...</option>
        <option value="BASIC">BASIC</option>
        <option value="C">C</option>
        <option value="C++">C++</option>
        <option value="Clojure">Clojure</option>
        <option value="COBOL">COBOL</option>
        <option value="ColdFusion">ColdFusion</option>
      </select>
    </div>    
  </div>
</div>