【发布时间】:2017-07-29 20:52:34
【问题描述】:
如何使加载按钮 java 函数在一个只有一个按钮的框中工作,而不是调用两个“不同”JavaScript 函数的两个单独按钮?
换句话说,澄清问题: 我有两个不同的函数需要触发,但其中一个可以触发我在选择框中选择的功能。
记住:警告和确认应该仍然有效,所以我不能为这两个选项使用相同的加载函数,因为不同的警告和确认。
注意:如果有可能不使用 jquery,只使用 JavaScript,那就太好了,但是如果有 jquery 的解决方案,那么只要因为它的工作原理。
这是我的 JavaScript 代码:
function loadFileAsTextUNLIMITED()
{
valid = true;
if(document.fileTextBox.fileLoadName.value == "")
{
alert ("Please attach a file to upload")
valid = false;
}
{
if (confirm("Are you sure you want upload unknown file? Remember that if you uploaded a file that is not a text or a web page source code file, it may hang up your computer or it may destroy the file when you try to save it. So if you chose to upload it this way, you are doing it on your own risk.") )
{
var fileToLoad = document.getElementById("fileToLoad").files[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var textFromFileLoaded = fileLoadedEvent.target.result;
document.getElementById("inputTextToSave").value = textFromFileLoaded;
};
fileReader.readAsText(fileToLoad, "UTF-8");
} else {
window.alert("Good, it is advised you use the normal Load Text button and this button only if you have too. ")
}
}
}
function loadFileAsText()
{
valid = true;
if(document.fileTextBox.fileLoadName.value == "")
{
alert ("Please attach a file to upload")
valid = false;
}
{
var fup = document.getElementById('fileToLoad');
var fileName = fup.value;
var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
if(ext == "TXT" || ext == "txt" || ext == "HTML" || ext == "html" || ext == "HTM" || ext == "htm" || ext == "JS" || ext == "js" || ext == "CSS" || ext == "css" || ext == "PHP" || ext == "php" || ext == "DOWNLOAD" || ext == "download")
{
var fileToLoad = document.getElementById("fileToLoad").files[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var textFromFileLoaded = fileLoadedEvent.target.result;
document.getElementById("inputTextToSave").value = textFromFileLoaded;
};
fileReader.readAsText(fileToLoad, "UTF-8");
}
else
{
alert("Upload a text or a web page file with source codes");
fup.focus();
return false;
}
}
}
这是我的 HTML 代码的那些部分:
<input type="file" id="fileToLoad" name="fileLoadName">
<input type="button" onclick="loadFileAsText();showFileSize();" value=" Load Text / Source Code Files ">
<br>
<input type="button" onclick="loadFileAsTextUNLIMITED();showFileSize();" value=" On your own risk Load Any File Type">
<br>
<textarea id="inputTextToSave" name="inputTextToSave" rows="10" cols="70" placeholder="Type your text here:"></textarea>
【问题讨论】:
标签: javascript java jquery html upload