【问题标题】:How to get onchange to set onclick=window.open(url)?如何让 onchange 设置 onclick=window.open(url)?
【发布时间】:2015-03-14 05:52:45
【问题描述】:

我在 GAS html 服务中有一个应用程序,带有一个文件选择框,旁边有一个按钮,用于在新选项卡中打开它们。我不知道如何完成它。列表中的文件以 google-drive-file-id 形式获取它们的值(假设 fileID1-3 没问题),并且我有一个用于获取整个链接的服务器脚本。以下是它的完成方式:

HTML:

<select id='fileBox' name='fileBox' onchange="google.script.run.withSuccessHandler(gotFileLink).getFileLinkById(this.value)">
<option value=fileID1>File1.pdf</option>
<option value=fileID2>File2.pdf</option>
<option value=fileID2>File3.pdf</option>
</select>
<input type="button" value="Open File" id="linkButton" />

服务器代码:

function getFileLinkById(fileID) { return DriveApp.getFileById(fileID).getUrl(); }

客户端代码:

function gotFileLink(url) {
document.getElementById('linkButton').onclick = // what goes here?
}

我已经尝试了几个使用“window.open”的选项,但不知道如何使它工作。 提前致谢。

【问题讨论】:

  • 有类似的问题,带有选项的本机选择,例如,打印,在新窗口/选项卡中打开 pdf,并且更改事件不假设用户交互信任,至少在 Safari 中,以及新的标签被阻止:(

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


【解决方案1】:

这是可以帮助您在单击按钮时打开所需链接的代码。

这是一个有效的链接: JSFIDDLE

<select id='fileBox' name='fileBox'>
<option value='http://www.google.com'>File1.pdf</option>
<option value='http://www.google.com'>File2.pdf</option>
<option value='http://www.google.com'>File3.pdf</option>
</select>
<input type="button" value="Open File" id="linkButton" />


var btn = document.getElementById('linkButton');
btn.addEventListener('click',GetInfo,false);

function GetInfo(){
var e = document.getElementById("fileBox");
var selectedUrl = e.options[e.selectedIndex].value;
window.open(selectedUrl);  
}

【讨论】:

  • 不完全是我正在寻找的答案,但你给了我一种看待问题的新方法,我解决了它。谢谢!
【解决方案2】:

回答我自己的问题,但基于 maxspan 的解决方案(这不是我想要的),我能够以另一种方式解决这个问题:

<select id='fileBox'>
<option value=fileID1>File1.pdf</option>
<option value=fileID2>File2.pdf</option>
<option value=fileID2>File3.pdf</option>
</select>
<input type="button" value="Open File" id="linkButton" onclick="runner.withSuccessHandler(window.open).getFileLinkById(document.getElementById('fileBox').value)" />

如果有人有不同/更好的答案 - 我仍然想听听...感谢 maxspan 帮助我。

【讨论】:

    猜你喜欢
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多