单击提交按钮时,它没有给出适当的结果

时间:2013-07-17 21:26:24

标签: javascript html excel

你好,                   我在使用javascript和amp;时遇到了一些问题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..

2 个答案:

答案 0 :(得分:0)

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

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

如果删除它,则不会出现此行为。

答案 1 :(得分:0)

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

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