Javascript:从输入字段接收数据

时间:2016-06-06 12:43:15

标签: javascript jquery google-apps-script

如何使用Google Scripts App中的JavaScript从输入字段接收数据?

例如:我在html页面上有一个输入字段,其中输入了一些文本,当我按下一个按钮时,应该将数据发送到.gs文件中的变量以供进一步使用。

这是我的代码:

HTML:

<html>
  <head>
    <base target="_top">
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  </head>
  <body>
    <h1 class="center">
      <b>App Title</b>
    </h1>
    <div class="form-auth">
      <label class="inline">Authorization</label>
      <input class="auth-input" placeholder="Insert Authorization"/>
    </div>
    <button class="btn-default start">Start</button>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
        $('.start').click(function() {
          alert($('.auth-input').val());
        });
    </script>
  </body>
</html>

GS

function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .createMenu('Menu')
      .addItem('Show sidebar', 'showSidebar')
      .addToUi();
}

function showSidebar() {
  var html = HtmlService.createHtmlOutputFromFile('login')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME)
      .setTitle('SDR Tag Governance')
      .setWidth(300);
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .showSidebar(html);
}

function getValue() {
  //add the value from input field to a variable
}

1 个答案:

答案 0 :(得分:1)

只需在按下的回车键上添加一个事件,然后将该值传递给该函数。例如getValue($('。auth-input')。val())。当然,您需要将getValue的定义更改为:function getValue(val){}