简体   繁体   English

使用Java脚本从下拉列表中打开.xls文件

[英]Using Javascript to open an .xls file from a drop-down list

Cannot find anywhere how to make a drop-down list open a .xls file. 在任何地方都找不到如何使下拉列表打开.xls文件的地方。

What my code is: 我的代码是什么:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript"> function download(d) { if (d !== '') { window.open('URL goes here/' + d); } } </script> </head> <body> Excel List: <select name="download" onchange="download(this.value)" style="width:150px;" > <option value="">Select an Excel list</option> <option value="file_name1.xls">File name</option> <option value="file_name2.xls">File name</option> </select> </body> </html> 

When I do this, it opens 4 additional tabs and then asked if I want to open or save the excel file. 当我这样做时,它将打开4个其他选项卡,然后询问我是否要打开或保存Excel文件。 How can I make it work by choosing the excel list and not having the 4 additional tabs open and only show to open or save the file? 如何通过选择excel列表而不打开4个其他选项卡而只显示打开或保存文件的方式使其工作?

The key to your question is the window.open() method. 您问题的关键是window.open()方法。 It is documented here . 它记录在这里 What this method does is open a new window using the first parameter as the name of the URL to load in the new window. 此方法的作用是使用第一个参数作为要在新窗口中加载的URL的名称打开一个新窗口。 The window.open() method can take a value as parameter two that determines where the new browser window should be loaded. window.open()方法可以将值用作参数2,该值确定应在何处加载新的浏览器窗口。 The default is _blank which opens a new window (tab) which is what you are seeing. 默认值为_blank ,它将打开一个新窗口(选项卡),您将看到该窗口。 It sounds like you may want to supply _self to replace the current page with the selected file. 听起来您可能想提供_self以将当前页面替换为所选文件。

Your new code would then become: 您的新代码将变为:

function download(d) {
    if (d !== '') {
        window.open('URL goes here/' + d, '_self');
    }
}

You just need to use '_self'. 您只需要使用'_self'。

window.open('URL goes here/' + d, '_self'); window.open('URL转到此处/'+ d,'_self');

OR 要么

window.location.href = 'URL goes here/' + d; window.location.href ='URL转到此处/'+ d;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JavaScript,下拉列表中的“填充”复选框从文本文件更改 - Using JavaScript, Populate checkboxes on drop-down list change from a text file 使用JavaScript而不是HTML创建下拉列表 - Create drop-down list using JavaScript instead of HTML 使用Javascript HTML和jQuery的下拉列表 - Drop-Down List using Javascript HTML and jQuery 使用javascript对JSON文件或至少一个下拉列表进行排序的另一种方法? - Alternative way to sort JSON file or at least, a drop-down list using javascript? 使用JavaScript将Java.util.List与下拉列表绑定 - Binding java.util.List with drop-down using Javascript JavaScript:使用另一个下拉列表中的onchange数据填充html下拉列表 - Javascript: Populating a html drop-down list based on onchange data from another dropdown using 在不使用 JavaScript 的情况下根据下拉列表中的选择转发到新页面 - Forward to a new page based on the selection from a drop-down list without using JavaScript 根据使用 javascript 从下拉列表中的选择更改 php 中的表单元素 - Change elements of a form in php based on the selection from a drop-down list using javascript 如何验证是否使用JavaScript从下拉列表中未选择任何内容 - how to validate if nothing is selected from drop-down using JavaScript 使用内联JavaScript更新下拉列表 - Updating a drop-down list with inline javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM