jQuery Accordion如何进入选择活动标题的按钮

时间:2014-11-25 14:59:38

标签: javascript jquery jquery-ui google-apps-script

我在对话框中使用手风琴来完成最终用户需要执行的不同步骤。每个步骤都有一个带按钮的按钮或文本框。如何获取它,以便当用户点击回车键时,它会激活与活动标题关联的按钮。

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">

<div id="accordion">

    <h3 id="Step1">Step 1:</h3>
  <div>
    <p>
    To get started click "Setup" and it will configure this spreadsheet for use with the Connected Groups Add-on.
    </p>

    <p><button id="Step1B" class="action" onclick="step1()">Setup</button></p>
  </div>

  <h3 id="Step2">Step 2</h3>
  <div>
    <p>
    To get started enter the name of your first contact group below then click "Create". 
    </p>
    <p><input type="text" id="gName">
    <button id="Step2B" class="action" onclick="step2()">Create</button></p>
  </div>
  <h3  id="Step3">Done</h3>
  <div>
    <p><center>
    Thank you for using Connected Groups.  More information or help can be found:
    </center>
    </p>
    <p><button id="thankYou" onclick="step3()">Close</button></p>
  </div>

</div>

  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>

  <script>
 $(function() {
    $( "#accordion" ).accordion({
      heightStyle: "content"
    });  
  }); // run on launch

  function step1(){
  alert("Step1");
  $('#Step2').click(); 
  };

    function step2(){
    var tb = $("#gName").val();
  alert("Step2: "+tb);
  $('#Step3').click(); 
  };

   function step3(){
  google.script.host.close();
  };

  </script>

1 个答案:

答案 0 :(得分:1)

  

如何获取它,以便当用户点击回车键时,它会激活与活动标题相关联的按钮?

首先,我们可以捕获输入元素的按键事件。如果按键是输入&#39;密钥,然后我们会将click()发送给“活跃的&#39;按钮。我们将其存储在全局变量window.actionButton中。

$(':input').keyup(function (event) {
    if (event.keyCode == 13) {
        window.actionButton.click();
    }
});

如果我们假设每个面板都包含class='action'的单个按钮,那么我们就可以跟踪“活跃”按钮。按钮。

window.actionButton = ui.newPanel.find(".action");

每当选择标题(+面板)时,activate事件都会触发。在页面加载期间,我们将函数绑定到该操作,并让它更新window.actionButton

activate: function (event, ui) {
    window.actionButton = ui.newPanel.find(".action");
},

如果我们移动焦点以突出显示预期的操作,工作流程会得到改善。理想情况下,焦点应该转到下一个输入,无论是什么,但在这个简单的手风琴中它足以移动到适当的按钮:

window.actionButton.focus();

不幸的是,在Google Apps脚本HtmlService中运行时,我们可以定位程序化按钮点击但无法控制焦点:Cannot simply force focus on text input in stand-alone Google App using HtmlService?您会发现jsFiddle的操作方式与HtmlService中的操作方式不同。 / p>

脚本

Working jsFiddle

<div id="accordion">
     <h3 id="Step1">Step 1:</h3>

    <div>
        <p>To get started click "Setup" and it will configure this spreadsheet for use with the Connected Groups Add-on.</p>
        <p>
            <input type="button" id="Step1B" class="action" value="Setup" />
        </p>
    </div>
     <h3 id="Step2">Step 2</h3>

    <div>
        <p>To get started enter the name of your first contact group below then click "Create".</p>
        <p>
            <input type="text" id="gName" />
            <button id="Step2B" class="action">Create</button>
        </p>
    </div>
     <h3 id="Step3">Done</h3>

    <div>
        <p>
            <center>Thank you for using Connected Groups. More information or help can be found:</center>
        </p>
        <p>
            <button id="thankYou" class="action">Close</button>
        </p>
    </div>
</div>


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<script>
  /**
   * On document load, set up accordion & event handlers
   */
  $(function() {
     $("#accordion").accordion({
         heightStyle: "content",
         // Handle activate event by setting a new "actionButton", 
         // and putting it into focus.
         activate: function (event, ui) {
             //alert(ui.newPanel.attr('id'));
             window.actionButton = ui.newPanel.find(".action");
             window.actionButton.focus();
         },
         // Likewise for create event
         // This doesn't set focus - why not?
         create: function  (event, ui) {
             //alert(ui.panel.attr('id'));
             window.actionButton = ui.panel.find(".action");
             window.actionButton.focus();
         }
     });

     // Click handlers, assigned upon load
     $('#Step1B').click(function () {
         alert("Step1");
     });

     $('#Step2B').click(function () {
         var tb = $("#gName").val();
         alert("Step2: " + tb);
     });

     $('#Step3').click(function () {
         google.script.host.close();
     });

     // Setup keypress event handler for all inputs 
     // If 'enter' is pressed, click the current actionButton.
     $(':input').keyup(function (event) {
         if (event.keyCode == 13) {
             window.actionButton.click();
         }
     });
  });

</script>