0

您好,我在使用 javascript 和 Html 时遇到了一些问题。在我的代码中,我正在从目录中读取 excel 文件并显示一些输出。为此,我有一个文件输入类型和一个按钮...这里发生的事情是当我选择xls文件并单击提交按钮每次单击提交按钮时选择一个文件寡妇正在打开..代码如下

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Speedometer HTML5 Canvas</title>
    <script src="script copy.js"></script>
</head>
<body onload='draw(0);'>
    <canvas id="tutorial" width="440" height="220">
            Canvas not available.
        </canvas>
    <div>
        <form id="drawTemp">
        <input type="text" id="txtSpeed" name="txtSpeed" value="20" maxlength="2" />
        <input type="button" value="Draw" onclick="drawWithInputValue();">
        <input type=file id="fileInput" value="">
        <input type=button value="submit" onclick="readdata(1,1);">
        </form>
    </div>
</body>
</html>
<script type="text/javascript" language="javascript">
   function checkfile(sender) {
        var validExts = new Array(".xlsx", ".xls", ".csv");
        var fileExt = sender.value;
        fileExt = fileExt.substring(fileExt.lastIndexOf('.'));
        if (validExts.indexOf(fileExt) < 0) {
            alert("Invalid file selected, valid files are of " +
               validExts.toString() + " types.");
            return false;
        }
        else return true;

    }
var xVal = 1;
var yVal = 2

    function readdata(x,y) {
        x = xVal;
        y = yVal;

        // Use the <input type='file'...> object to get a filename without showing the object.
        document.all["fileInput"].click();
        var fileName = document.all["fileInput"].value;
        try {
           var excel = new ActiveXObject("Excel.Application");
            excel.Visible = false;
            var excel_file = excel.Workbooks.Open(fileName);

            var excel_sheet = excel_file.Worksheets("Sheet1");
            var data = excel_sheet.Cells(x, y).Value;
            //alert(data);
            drawWithexcelValue(data);
            xVal = xVal + 1;
        }
        catch (ex) {
            alert(ex);
        }
    }
</script>
   Every time i click on the submit button it opens the choose window to choose file ..why my button is behaving like file type input..
4

2 回答 2

0

由于 Javascript 中的这一行,它正在打开“选择文件”对话框:

document.all["fileInput"].click();

如果你删除它,你将不会得到这种行为。

于 2013-07-17T21:35:43.260 回答
0

document.all["fileInput"].click();

当您单击提交时,readdata() 函数中的这一行是以编程方式单击选择文件按钮。

于 2013-07-17T21:36:04.140 回答