2010-03-08 42 views
0

**沒關係。我想到了。 **jQuery手風琴 - 爲活躍的手風琴設置輸入焦點?

我做了這樣的:

$("#accordion").accordion({ 
     header:'h3', 
     active: '#section1', 
     autoheight: false, 
     clearstyle: true, 
}).bind("change.ui-accordion", function(event,ui) { 
    $("#text1").focus(); 
}); 

我有一個可摺疊的所有設置,每個div有內它的形式。我只是想弄清楚如何設置注重根據輸入字段上哪一個是開放...

/* accordion */ 
$("#accordion").accordion({ 
     header:'h3', 
     active: '#section1', 
     autoheight: false, 
     clearstyle: true 
}); 

基本上,我想設置在哪個部分是第一個輸入字段中的光標打開。實際的形式是更大的,所以我就凝結極大...

<div id="accordion"> 
     <h3 id="section1"><a href="#">Section 1/a></h3> 
     <div> 
      <form id="form1" action="form.php" method="post"> 
       <fieldset class="inside"> 
        <input type="text" name="text1" id="text1" size="50" value="Default text" /> 
        <input class="open" type="button" value="Submit" name="submit1" /> 
       </fieldset> 
      </form> 
     </div><!--/div--> 

     <h3 id="section2"><a href="#">Section 2</a></h3> 
     <div> 
      <form id="form2" action="form.php" method="post"> 
       <fieldset class="inside"> 
        <input type="text" name="text2" id="text2" size="50" value="Submit" /> 
        <input class="open" type="button" value="Submit" name="submit2" /> 
       </fieldset> 
      </form> 
     </div><!--/div--> 

     <h3 id="section3"><a href="#">Section 3</a></h3> 
     <div> 
      <form id="form3" action="form.php" method="post"> 
       <fieldset class="inside"> 
        <input type="text" name="text3" id="text3" size="50" value="Submit" /> 
        <input class="open" type="button" value="Submit" name="submit3" /> 
       </fieldset> 
      </form> 
     </div><!--/div--> 

回答

0
$("#accordion").accordion({ header:'h3', active: '#section1', autoheight: false, clearstyle: true, }).bind("change.ui-accordion", function(event,ui) { $("#text1").focus(); }); 

這不會爲其他子手風琴工作。正如你對文本框的ID進行硬編碼一樣。

在change.ui-accordion事件中可能會涉及到某些東西,但我對它並不十分熟悉。你可以使用這樣的東西:

$(document).ready(function() { 
     $("div#accordion > h3 > a").click(function(e) { // when we click a link 

      e.preventDefault(); // prevent the click from bubbling 

      var parenth3 = $(this).parent(); // find the parent h3 the sender is in 

      //this selector then finds the first textbox that is in the div adjacent to the parent h3. 
      $("#" + parenth3[0].id + " + div > form > fieldset.inside > input[type=text]:first").focus(); 

     }); 
    }); 

這對我來說感覺很不好意思。

編輯:另請注意,您的第1部分的定位標記未正確關閉。