javascript - 使用 .withSuccessHandler 更改输入按钮的属性

标签 javascript jquery-ui google-apps-script

我正在尝试使用 HTML 服务在 Google Apps 脚本中构建用户界面。我希望我的按钮从“保存”更改为“已保存!”当数据成功保存到用户的脚本属性时。我知道函数 saveProperties 正在成功执行,因为正在更新用户属性。不知道为什么我正在做的事情不起作用。该按钮仍然显示“保存”。

脚本文件

function saveProperties(first, last, email) {  
  ScriptProperties.setProperty('first', first);
  ScriptProperties.setProperty('last', last);
  ScriptProperties.setProperty('email', email);
  return;
}

html 文件

<input type="text" id="first"/>
<input type="text" id="last"/>
<input type="text" id="email"/>
<input id="submitButton" type="submit" value="Save"/>

<script>
function onSubmit(){
    var $first = $('#first').val();
    var $last = $('#last').val();
    var $email = $('#email').val();
    google.script.run
    .withSuccessHandler(changeButton)
    .saveProperties($first, $last, $email);
}

function changeButton(test){
    $('#submitButton').attr('value','SAVED!');
}
</script>

最佳答案

我的测试不包括jQuery等外部库,但应该没有区别。

代码.gs

function saveProperties(first, last, email) {  
  ScriptProperties.setProperty('first', first);
  ScriptProperties.setProperty('last', last);
  ScriptProperties.setProperty('email', email);
  return;
}

function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename).getContent();
}

JavaScript.html

<script>
function onSubmit(obj) {
    var $first = document.getElementById('first').value;
    var $last = document.getElementById('last').value;
    var $email = document.getElementById('email').value;
    google.script.run.withSuccessHandler(changeButton).withUserObject(obj).saveProperties($first, $last, $email);
}

function changeButton(value, obj) {
    obj.value = 'SAVED!';
}
</script>

Test.html

<input type='text' id='first'/>
<input type='text' id='last'/>
<input type='text' id='email'/>
<input id='submitButton' name='submitButton' type='button' value='Save' onclick='onSubmit(this)' />
<?!= include('JavaScript'); ?>

更新 - 替代版本:

JavaScript.html

<script>
function onSubmit() {
    var $first = document.getElementById('first').value;
    var $last = document.getElementById('last').value;
    var $email = document.getElementById('email').value;
    google.script.run.withSuccessHandler(changeButton).saveProperties($first, $last, $email);
}

function changeButton() {
    document.getElementById('submitButton').value = 'SAVED!';
}
</script>

Test.html

<input type='text' id='first'/>
<input type='text' id='last'/>
<input type='text' id='email'/>
<input id='submitButton' name='submitButton' type='button' value='Save' onclick='onSubmit()' />
<?!= include('JavaScript'); ?>

关于javascript - 使用 .withSuccessHandler 更改输入按钮的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21501459/

相关文章:

javascript - 在 Chrome 中调整 div 大小的问题

jquery-ui - 动态填充 JQuery 数据表

javascript - 停止网页滚动

jquery ui 自动完成 css

google-apps-script - 按特定顺序对 Google 电子表格单元格求和

javascript - 更改 jquery 模式确认对话框的大小

javascript - 深入理解: What is the underlying logic behind the way array indexes work?

google-apps-script - Google Apps 脚本,脚本已完成但未返回任何内容

javascript - 在不重新加载侧边栏的情况下更新 Google Apps 脚本侧边栏中的内容

javascript - JavaScript 如何解释索引?