簡體   English   中英

使用Java腳本從下拉列表中打開.xls文件

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

在任何地方都找不到如何使下拉列表打開.xls文件的地方。

我的代碼是什么:

 <!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> 

當我這樣做時,它將打開4個其他選項卡,然后詢問我是否要打開或保存Excel文件。 如何通過選擇excel列表而不打開4個其他選項卡而只顯示打開或保存文件的方式使其工作?

您問題的關鍵是window.open()方法。 它記錄在這里 此方法的作用是使用第一個參數作為要在新窗口中加載的URL的名稱打開一個新窗口。 window.open()方法可以將值用作參數2,該值確定應在何處加載新的瀏覽器窗口。 默認值為_blank ,它將打開一個新窗口(選項卡),您將看到該窗口。 聽起來您可能想提供_self以將當前頁面替換為所選文件。

您的新代碼將變為:

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

您只需要使用'_self'。

window.open('URL轉到此處/'+ d,'_self');

要么

window.location.href ='URL轉到此處/'+ d;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM