作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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”的选项,但不知道如何让它工作。提前致谢。
最佳答案
这是可以帮助您在单击按钮时打开所需链接的代码。
这是一个工作链接: 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);
}
关于javascript - 如何让 onchange 设置 onclick=window.open(url)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29045928/
我是一名优秀的程序员,十分优秀!