我正在尝试将PDFObject用于我的手册,我希望用户选择车辆并下载她/他选择的特定车辆。这是我的代码:

<form method="get" action="">

  <div class="form-group"><h5>Model</h5></div>

  <div id="select-vehicle" class="form-group">
    <select class="form-control">
      <option selected disabled> Select Vehicles</option>
    </select>
  </div>

  <div class="form-group">
    <button type="submit" value="submit" id="submit" disabled class="btn btn-default">DOWNLOAD</button>
  </div>

</form>


这是我的工作javascript:

var workers = ["86", "Altis", "Avanza", "Altis", "Camry", "Fortuner", "FJ Cruiser"
            , "Hiace", "Hilux", "Innova", "LandCruiser", "Prado", "Previa", "PriusC"
            , "Rav4", "Vios", "Yaris"];
        for(var i=0; i< workers.length;i++)
        {
        //creates option tag
          jQuery('<option/>', {
                value: workers[i],
                html: '2017' + ' ' + workers[i] + ' ' + '&#174;'
                }).appendTo('#select-vehicle select'); //appends to select if parent div has id dropdown
        }
          $('button').click(function(event) {
            var value = $( "select" ).val();
            $('form').attr('action', '../../vehicles/' + value +'/');
          });

        $('select').change(function(){
          if ($(this).val())
          {
            $("button").removeAttr('disabled');
          }
        });


我想将此$('form').attr('action', '../../vehicles/' + value +'/');传递给PDFObject,以便它将寻找正确的文件名来下载文件。

最佳答案

下载PDF:

PDFObject用于在页面中嵌入pdf。要下载pdf,您可以直接转到该pdf,但是您需要pdf文件名。您没有显示pdf文件的名称,因此这里有一个示例,说明文件名与select的值相同...

$('button').click(function(e) {
    e.preventDefault();
    var value = $('select').val();
    window.open('../../vehicles/' + value + '/' + value + '.pdf','_blank');
});


嵌入PDF:

如果要使用PDFObject将pdf嵌入页面中(此选项不需要form,只需select),请首先在html中创建一个容器来保存该pdf ...

<div id="vehiclepdf"></div>


...并在单击按钮时调用PDFObject ...

$('button').click(function(e) {
    e.preventDefault();
    var value = $('select').val();
    PDFObject.embed('../../vehicles/' + value + '/' + value + '.pdf',"#vehiclepdf");
});

09-25 16:27