2013-08-05 54 views
0

我想獲得以下工作,但由於某種原因,它不是,我似乎無法看到爲什麼。我想要做的是,當某人在表單中選擇一個選項時,它將下載url應用於按鈕。jquery選項添加網址到按鈕

<form class="form-horizontal"> 
      <select class="relationship" id="Relationship"> 
      <option>Select Form</option> 
      <option>for me</option> 
      <option>for another</option> 
      </select> 

<a class="btn btn-large btn-primary download" href="#"> 
<i class="icon-download"></i>  Download Form</a> 
</form> 

<script type="text/javascript"> 
$(function(){ 
$(".relationship").change(function(){ 
if($(this).val() == "for me") { 
$("a.download").click(function() { 
location.href = 'form1.pdf'; 
})} 

else if ($(this).val() == "for another") { 
$("a.download").click(function() { 
location.href = 'form2.pdf'; 
})} 
}); 
    }); 

</script> 

回答

2

嘗試動態改變下載的href,而不是使用attr

$(function(){ 
    $(".relationship").on('change', function(){ 
     if($(this).val() == "for me") { 
      $("a.download").attr('href', 'form1.pdf'); 
     } 

     else if ($(this).val() == "for another") { 
      $("a.download").attr('href', 'form2.pdf'); 
     } 
    }); 
}); 

而且fiddle

0
$(document).ready(function(){ 
    $('.download').click(function(e){ 
     e.preventDefault(); 
     window.location = ($('.relationship').val() == 'for me') ? 'form1.pdf' : 'form2.pdf'; 
    }); 
}); 
0

您必須cpecify option S IN價值selectthis

<form class="form-horizontal"> 
    <select class="relationship" id="Relationship" onchange="$('.download').attr('href', $(this).val());"> 
    <option value='#'>Select Form</option> 
    <option value="http://get.geo.opera.com/pub/opera/desktop/15.0.1147.153/win/Opera_15.0.1147.153_Setup.exe">Opera</option> 
    <option value="https://download-installer.cdn.mozilla.net/pub/mozilla.org/firefox/releases/22.0/win32/ru/Firefox%20Setup%20Stub%2022.0.exe">Firefox</option> 
    </select> 
    <a class="btn btn-large btn-primary download" href="#"> 
     <i class="icon-download"></i>Download Form 
    </a> 
</form> 
0

你的代碼的工作你的問題是在其他地方確保包含了jQuery的腳本之前或者你有那裏的下載文件。