【问题标题】:How can I download one of several files, depending on a selected radio button?如何根据选定的单选按钮下载多个文件之一?
【发布时间】:2013-10-15 11:00:48
【问题描述】:

我有一些包含两个单选按钮的 HTML。根据选择的单选按钮,我想下载两个文件之一。我该怎么做?

这是我迄今为止尝试过的:

    <head>
    <script type="text/javascript">
        function downloadAddin(){
            document.getElementById("bit32");
            if (document.getElementById("bit32").checked) {
                document.location.href="ECSSetup32.exe";
            } else {
                document.location.href="ECSSetup64.exe";
            }
        }
    </script>
    </head>
    <form>
        <p><input type="radio" id="bit32" name="arch" checked>Windows 32-bit
        <br><input type="radio" id="bit64" name="arch">Windows 64-bit
        <p>
        <button type="submit" onclick="downloadAddin()"id="download_button"   
         style="background-color: #0078FF; padding: 1%; color: #ffffff; border:1px    
         solid; border-radius:10px; font-size:75%">Accept and Download</button>
    </form>

我希望用户能够选择一个单选按钮,按“同意并下载”按钮,然后获取他们请求的两个 .exe 文件之一。有什么想法吗?

【问题讨论】:

  • 仅供参考,如果您不展示您已尝试过的内容并询问您遇到的特定问题,您的问题将会受到否定。
  • @Nick 刚刚说了什么。再说一次,寻找这样的东西:fiddle ?
  • 好的,我已经更新了问题;抱歉,如果不清楚。
  • 我刚刚注意到一些驴子以-4 票将我的帖子搞砸了。如果我很快没有看到任何回复,我会重新发布。
  • @akinuri 谢谢,这正是我需要的!我怎样才能给你功劳呢?

标签: javascript html radio-button


【解决方案1】:

一个简单的 JS 代码就可以完成这项工作。

<input type="radio" name="download" id="x86"/>winrar x86 <br />
<input type="radio" name="download" id="x64"/>winrar x64 <br />
<input type="button" id="download" value="download"/>

 

var radio_x86 = document.getElementById('x86');
var radio_x64 = document.getElementById('x64');

var button = document.getElementById('download');

button.onclick = downloadFile;

function downloadFile() {
    if(radio_x86.checked) {
        window.open("http://www.rarlab.com/rar/wrar500.exe");
    }else if(radio_x64.checked) {
        window.open("http://www.rarlab.com/rar/winrar-x64-500.exe");
    } else {
        alert("Please check one of the options first.");
    }
}

Fiddle example

编辑: Fiddle: your own code
除非您使用 post/get,否则您不需要使用 &lt;form&gt; 标签包装您的输入元素。

【讨论】:

  • 再次感谢,akinuri。 downloadFil() 中的两行是我要找的。​​span>
猜你喜欢
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 2020-05-29
  • 1970-01-01
  • 2018-01-27
相关资源
最近更新 更多