使用复选框同时下载多个文件

时间:2011-05-24 19:52:05

标签: html

我有一个HTML页面,其中包含以下代码:

<html>
<head>
<script language="JavaScript" type="text/javascript"> 

function validate(frm)
{
  var count = 2;
  var allChecked = false;
  for(i=1; i<=count; i++)
  {
    if( document.getElementById('track_'+i).checked == true)
    {
        allChecked = true;
        break;
      }
  }
    if(!allChecked)
    {
        alert("Please select atleast one track");
    }
    return false;
}

</script>
</head>

<body>

<form method="get" onSubmit="validate(this);">

<input type="checkbox" value="01 - Sun Beliya - Lata, SP Balasubramaniam @ Fmw11.com.mp3" id="track_1" name="track_1"><a href ="01 - Sun Beliya - Lata, SP Balasubramaniam @ Fmw11.com.mp3">01 - Sun Beliya - Lata, SP Balasubramaniam @ Fmw11.com.mp3  </a> <br>
<input type="checkbox" value="02 - Gabbar Singh Yeh Kah - Alka Yagnik, Amit Kumar @ Fmw11.com.mp3" id="track_2" name="track_2"><a href ="02 - Gabbar Singh Yeh Kah - Alka Yagnik, Amit Kumar @ Fmw11.com.mp3">02 - Gabbar Singh Yeh Kah - Alka Yagnik, Amit Kumar @ Fmw11.com.mp3  </a><br>
<input type="submit" value="Download" >
</form>

</body>
</html>

我想同时下载这两个文件,即当选中这两个复选框时,应该下载这两个文件。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

尝试使用:

<html>
<head>
<script language="JavaScript" type="text/javascript"> 

function validate(frm)
{
  var SomeChecked = false;
  for(var i = 0; i < tracks.length; i++)
  {
      if(tracks[i].checked) 
      {
          SomeChecked = true;
          var url= tracks[i].value;    
          window.open(url,'Download');  
      }
  }
  if(!SomeChecked)
  {
      alert("Please select atleast one track");
  }
  return false;
}

</script>
</head>

<body>

<form method="post" onSubmit="validate(this);">

<input type="checkbox" value="01 - Sun Beliya - Lata, SP Balasubramaniam @ Fmw11.com.mp3" id="track_1" name="tracks"><a href ="01 - Sun Beliya - Lata, SP Balasubramaniam @ Fmw11.com.mp3">01 - Sun Beliya - Lata, SP Balasubramaniam @ Fmw11.com.mp3  </a> <br>
<input type="checkbox" value="02 - Gabbar Singh Yeh Kah - Alka Yagnik, Amit Kumar @ Fmw11.com.mp3" id="track_2" name="tracks"><a href ="02 - Gabbar Singh Yeh Kah - Alka Yagnik, Amit Kumar @ Fmw11.com.mp3">02 - Gabbar Singh Yeh Kah - Alka Yagnik, Amit Kumar @ Fmw11.com.mp3  </a><br>
<input type="submit" value="Download" >
</form>

</body>
</html>

确保将复选框名称更改为“曲目”,它们应具有相同的名称但不同的ID。

每个复选框的值都会下载URL。

让我知道它是否适合你:)

答案 1 :(得分:1)

上面的Danpe非常接近正确的想法,但第二个if语句应该有更新的var名称:

 if(!allChecked) {
     alert("Please select at least one track");
 }

应该是:

 if(!Checked) {
     alert("Please select at least one track");
 }