使用Enter键提交从NATIVE转换为IFRAME沙箱 [英] Converting from NATIVE to IFRAME Sandbox with Enter Key Submit

查看:114
本文介绍了使用Enter键提交从NATIVE转换为IFRAME沙箱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试转换我的帖子时,我有与此帖子类似的问题apps脚本Web应用程序以使用IFRAME沙箱.我已经按照建议将其转换为'input ="button"'.

I have a similar problem to this post when trying to convert my apps script web app to use IFRAME Sandbox. I have converted to 'input = "button"' as suggested.

我的Web应用程序是一种简单的表单,供学生用来登录和退出学校图书馆.该应用程序的想法是让学生尽可能容易地使用它.学生应输入其身份证号,并可以单击提交按钮或按Enter键.他们的ID会先经过验证,然后再存储在电子表格中,并且会收到一条回信,表示感谢您登录或注销,或者请输入有效的ID号.然后,焦点应返回到文本框并清除,以备下一个学生输入其ID.

My web app is a simple form for students to use to sign in and out of a school library. The idea for the app is for it to be as easy as possible for students to use. Students should enter their id number and be able to either click the submit button or hit the enter key. Their ID is then validated before being stored in a spreadsheet and they get a message back saying thanks for signing in or out, or please enter a valid ID Number. Then focus should return to the text box and be cleared, ready for the next student to enter their id.

我使用NATIVE模式如上所述进行了工作.当我尝试将其转换为IFRAME模式时,单击该按钮即可,但是如果您按Enter键,则所有内容都将消失,并且没有数据进入电子表格. 如何按Enter键与单击提交"按钮相同?

I had it working as described above using NATIVE mode. When I tried to convert it to IFRAME mode, clicking the button works, but if you hit the enter key everything just disappears and no data goes to the spreadsheet. How can I get hitting the enter key to work the same as clicking the submit button?

index.html代码:

index.html code:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
</head>
<body>
<h3>Please Sign In &amp; Out</h3>
<div id="box" class="frame">
  <form id="signSheet" onsubmit="google.script.run
      .withSuccessHandler(updateInfo)
      .validateID(this.parentNode)">
    <input type="text" name="myID" id="myID" placeholder="Enter your student ID" autocomplete="off">
    <input type="button" value="Submit" onmouseup="google.script.run
      .withSuccessHandler(updateInfo)
      .validateID(this.parentNode)">
  </form>
  <span id="thank_you" hidden="true"></span>
</div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<?!= include('javascript'); ?>

</body>
</html>

javascript.html代码:

javascript.html code:

<script>
function updateInfo(ret){

        if(ret[0]){
          $( "#thank_you" ).removeClass("error");
          $( "#thank_you" ).addClass("valid");
        }
        else{
          $( "#thank_you" ).removeClass("valid");
          $( "#thank_you" ).addClass("error");
        }
        $( "#thank_you" ).text(ret[1]);
        $( "#thank_you" ).show("slow");
        $( "#signSheet" )[0].reset();
        $( "#myID" ).focus();
        console.log(ret);
      }

</script>

Code.gs代码:

Code.gs code:

//spreadsheet key is needed to access the spreadsheet.
var itemSpreadsheetKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

//Open the spreadsheet and get the sheet objects
var openedSS = SpreadsheetApp.openById(itemSpreadsheetKey);
var studentList = openedSS.getSheetByName("Student List");//Spreadsheet must match with sheet name
var studentRecord = openedSS.getSheetByName("Sign In-Out Record");

function doGet() {
  var html = HtmlService.createTemplateFromFile('index').evaluate()
               .setTitle('Sign In/Out Sheet')
               .setSandboxMode(HtmlService.SandboxMode.IFRAME);
  return html;
}

function include(filename) {
  Logger.log('enter include');
  Logger.log(filename);
  var html = HtmlService.createHtmlOutputFromFile(filename).getContent();
  Logger.log(html);
  return html;
}

function validateID(form){
  var idNum = form.myID;
  var valid = false;
  var numIdList = studentList.getLastRow()-1; //-1 is to exclude header row
  //get the item array
  var idArray = studentList.getRange(2,1,numIdList,1).getValues();
  i= idArray.length;
  while(i--){
    if(idArray[i][0]==idNum & idNum!='') {
      valid=true;
      break;
    }
  }
  if(valid)
    return [1, updateRecord(idNum)];
  else return [0, "ID number " + idNum + " not recognized. Please enter a valid ID number."];
}

function updateRecord(idNum){
        studentRecord.appendRow([idNum]);
        var formulas = studentRecord.getRange("B2:D2").getFormulasR1C1();
        var lRow = studentRecord.getLastRow();
        var range = studentRecord.getRange(lRow, 2, 1, 3);
        range.setFormulas(formulas);
        var vals = range.getValues();
        var now = new Date();
        studentRecord.getRange(lRow, 5, 1, 1).setValue(now);
        now = Utilities.formatDate(now, "EST", "HH:MM a");
        idNum = "Thanks " + vals[0][0] + ", you have signed " + vals[0][2] + " at " + now;
        return idNum;
  }

更新:我发现了这篇文章,并添加了将以下代码添加到javascript.html:

Update: I found this post and added the following code to javascript.html:

$(document).ready(function() {
  $(window).keydown(function(event){
    if(event.keyCode == 13) {
      var idVal = $("#myID").val();
      google.script.run.withSuccessHandler(updateInfo).validateID(idVal);
      return false;
    }
  });
})

这通过稍微调整index.html和Code.gs的部分为我解决了这个问题

This solved the the problem for me with a little more tweaking to parts of index.html and Code.gs

推荐答案

我发现

I found this post and added the following code to javascript.html:

$(document).ready(function() {
  $(window).keydown(function(event){
    if(event.keyCode == 13) {
      var idVal = $("#myID").val();
      google.script.run.withSuccessHandler(updateInfo).validateID(idVal);
      return false;
    }
  });
})

这侦听enter键,并将文本字段的值发送到apps脚本函数.在这种情况下,我不需要使用`event.preventDefault();'

This listens for the enter key and sends the value of the text field to the apps script function. In this case I didn't need to use `event.preventDefault();'

然后,我不得不调整按钮的onmouseup函数以使用this.parentNode.myID,并更改我的apps脚本函数以使用值而不是表单对象.

Then I had to adjust the button's onmouseup function to take this.parentNode.myID and change my apps script function to take a value instead of a form object.

这篇关于使用Enter键提交从NATIVE转换为IFRAME沙箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆