【问题标题】:How to Convert "Different" Multiple <Button> Functions to Generic Function with <Select>?如何使用 <Select> 将“不同的”多个 <Button> 函数转换为通用函数?
【发布时间】: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


    【解决方案1】:

    要获取选择的值(不确定它是单选按钮、下拉选择还是什么——我突然忘记了获取该值的语法,但很容易用谷歌搜索)查看这篇文章: Get selected value in dropdown list using JavaScript?

    一旦你有了这个值,你就可以编写一个提交(按钮点击)函数,看起来像这样:

     function uploadDocument() {
       var selectValue = //get the select value and store it here
       valid = true;
    
      if(document.fileTextBox.fileLoadName.value == ""){
        alert ("Please attach a file to upload")
        valid = false;
        return;
      } 
    
      if(selectValue === 'unlimited'){
        loadFileAsTextUNLIMITED()
      } else {
        loadFileAsText()
      }   
    }
    

    希望这更接近你想要完成的目标

    【讨论】:

    • 感谢您的回答,它确实有助于我清理代码。但它并没有回答如何使两个按钮作为一个按钮的&lt;select&gt; 框工作的问题? @HolyMoly
    • 也许我不明白你写的问题 - 所以澄清一下,你有两个不同的功能需要触发,但只需要一个按钮来触发这两个功能......对吗?
    • 是的,我现在将编辑问题,以便更清楚。功能不同又相似。
    • 但不能同时从同一个按钮触发。他们需要通过我从&lt;select&gt; 框中选择的&lt;option&gt; 从一个按钮触发一项功能。换句话说,函数应该由
    • 哦,我明白了。因此,您需要获取选择的值 - 并触发与该选择相对应的任何函数。我会很快更新我的答案。
    猜你喜欢
    • 2017-07-29
    • 1970-01-01
    • 2022-01-01
    • 2020-11-30
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    • 2022-12-06
    相关资源
    最近更新 更多