停止表单重新加载页面

时间:2013-07-11 17:47:25

标签: javascript html

我正在通过APACHE 2 Web服务器开发Web应用程序。当我执行我的dropbox.html文件时,它会生成一个使用PHP创建的htdocs目录中所有文件的下拉菜单。这是我的文件将被处理的地方。选择文件时,会调用javascript函数,这会经历一个使用perl脚本分析文件的漫长过程,依此类推。最终游戏是我希望生成图表并在处理完成时显示。在我有下拉菜单之前我有图表工作,我只是有一个调用这个相同的JS功能的按钮,它在一个硬编码文件上工作正常。这是我的问题:现在我的投递箱工作了,我可以看到图形弹出[正确],但它只会在整个页面重置回到起始形式[这将显示第一个]之前一秒钟文件在下拉菜单中]。如何阻止重置?即,当我从下拉菜单中点击某些内容时,它应该保留在该文件上,直到我更改它为止。我希望我的图表保持不变,直到用户选择要处理的另一个文件。

以下是我的dropbox.html文件的摘录。首先,我的下拉框代码。我遗漏了标准的html代码,只是向你展示了你需要的肉。

<!--The following is php code which gets the files in the current directory and 
populates a drop down menu with those files, it calls the openfilex method-->

<form name="filex" method="get" action="" onsubmit="openfilex()">
<select name="select">
<?php
$files = array_map("htmlspecialchars", scandir("."));
foreach ($files as $file)
    echo "<option value='$file'>$file</option>";
?>
</select>
<input type="submit" value="Choose File">
</form>

接下来调用它的JS函数。您可以看到我的JS函数调用perl脚本,将文件名作为参数传递,并从正在解析的perl脚本返回数据[所有pops,push]。当你开始看到HitCntGraph时,这些是对javascript库的调用我有plot.js,它是创建和绘制图形的方法。之前应该没有任何问题。

<script type="text/javascript">

//The following is the function associated with the event: file selected
function openfilex(){

var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function() {
    var str, wordCnt, word;
    if (xmlhttp.readyState==4 && xmlhttp.status==200) { // Get the results of the perl script
        // Get the complete string
        str = xmlhttp.responseText;
        str = str.substring(24);
        //alert(str);

        // Split it into the numberical results adding the keywords at the end as a long string
        wordCnt = str.split("-");
        //get the big string at the end
        word = wordCnt.pop();
        // Now split apart this big word into an array of the key words
        word = word.split(",");
        //we must pop the last element off since the string ended in an ',' which means there
        //is an extra element on this array [empty]
        word.pop();

        //word is now an array whose elements are each a keyword
        //alert(word);
        //wordCnt is now an array whose elements are each a keyword count
        //alert(wordCnt);

        var length = wordCnt[0]; //get the numer of xaxis ticks
        for(var i=0; i < length; i++) { //initialize the x-axis array 
            wordCnt[i] = wordCnt[i+1]; 
        } 

        //this will leave an extra element on the end [we essentially shrunk our array by one] so we pop this off.
        wordCnt.pop();

        // Create the graph [see the library]
        HitCntGraph = new MakeDraw();
        HitCntGraph.grid=1;
        HitCntGraph.enumerateP = 0;
        HitCntGraph.id="HitCntGraphCanvas";

        HitCntGraph.data = wordCnt; // data to plot amplitude
        HitCntGraph.dataUnit = " Hits    "; // few extra spaces make more space on the left

        HitCntGraph.dataColor = wordCnt; // data to plot colors
        HitCntGraph.dataColorUnit = ""; //we do not need an extra unit for color, so use the empty string

        HitCntGraph.horizontalArray = word; // my indices 

        HitCntGraph.enableMouseMove = 1;
        HitCntGraph.enableMouseDown = 1;
        //HitCntGraph.mouseMoveFunctionAssociated = showData;//triggerMouseMove; shows what data array index are you pointing on
        //HitCntGraph.mouseDownFunctionAssociated = showData;//triggerMouseDown;
        HitCntGraph.enumerateH = 1;
        HitCntGraph.title = "Keywords";
        // optional HitCntGraph.maximum = 255;
        // optional HitCntGraph.maximum = 255;HitCntGraph.minimum = 0.0001;
        HitCntGraph.plot();

    }
}

//get the filename that was selected from php code
var file = document.filex.select.value;

//call the perl script, passing the filename as a parameter
xmlhttp.open(
'GET', 
'dropdown.pl?name=' + encodeURIComponent(file),
false
);

xmlhttp.send();

}
</script>

表单,标题和JS函数都在我的脑海中。然后跟随身体部分,我想要绘制图形如下:

<body>
<script src="plot_v2.js"></script>
<canvas width="1000" height="300" id="HitCntGraphCanvas" style=""></canvas>
<!-- This div is only needed if mouseMoveFunctionAssociated is used -->
<div id="outputArea"></div>
</body>

所以,明智的,这与我的旧版本[在下拉菜单之前]相同。我已经测试了它的每一步,以确保所有组件正常工作。我走到最后,就像我说的那样,当我按下提交时,我甚至可以看到生成的CORRECT图表...虽然它只能保持一秒钟。我希望它保留在下拉菜单下方的屏幕上,直到用户选择另一个文件进行提交和处理。请帮忙?!

2 个答案:

答案 0 :(得分:2)

您的JS事件处理程序需要return false(或者,您可以在preventDefault()对象上调用event)以防止表单实际提交回页面(并导致页面刷新)

答案 1 :(得分:0)

您可以将<input type="submit" value="Choose File" />更改为<input type="button" value="Choose File" onClick="openfilex()" />,但之后您必须将文件名作为函数参数发送。