0

只有当用户选中复选框并提交表单时,我才希望下载 PDF。现在只要他们选中复选框并点击提交按钮,PDF 就会下载。表单是否提交并不重要。主要问题是我仅限于 Jquery 或纯 javascript。我无权访问后端文件。有人知道怎么做吗?不需要该复选框。这是我现在拥有的代码:

$("#formid").submit(function(ev){
                  ev.preventDefault();
                  $.ajax({
                      url: 'processing.cfc', // background processing procedures
                      type: 'get', 
                      dataType: 'json', 
                      data: $("#formid input, #formid select").serialize(), //pass all present input variables
                      success: formReturn, 
                      failure: function(){ alert("There was an error processing your request. \nPlease try again."); return false; }
                  }); 
                  var $choice = $(this).find("input[name='checkbox1']:checked");//get the selected option 
                    if ($choice.length)// if an option is selected
                    window.open('http://whitepaper.com/info/whitepaper/WhyBuy_WhitePaper_FinalB.pdf') ;

                  var $choice2 = $(this).find("input[name='checkbox2']:checked");//get the selected option 
                    if ($choice2.length)// if an option is selected
                    window.open('http://brochure.com/info/brochure/welcome-kit-brochure.pdf') ;
              });

这是复选框的 HTML:

<div class="chkbox">
  <input type="checkbox" class="regular-checkbox" name="checkbox1" 
  id="checkbox-1-1"><label for="checkbox-1-1"></label>&nbsp;&nbsp;
  <span class="dld">Clayton Homes guide to buying a home</span>
</div>
<div class="chkbox">
  <input type="checkbox" class="regular-checkbox" name="checkbox2" 
  id="checkbox-1-2"><label for="checkbox-1-2"></label>&nbsp;&nbsp;
  <span class="dld">Why Clayton Homes brochure</span>
</div>

任何帮助表示赞赏。

4

2 回答 2

1

您必须移动 PDF 打开 AJAX 函数的成功调用。

这里的关键:AJAX 调用是异步的,因此 PDF 将在 AJAX 请求被触发后立即打开,而不是在响应到达后打开。像这样的东西:

$("#formid").submit(function (ev) {
    ev.preventDefault();
    $.ajax({
        url: 'processing.cfc', // background processing procedures
        type: 'get',
        dataType: 'json',
        data: $("#formid input, #formid select").serialize(), //pass all present input variables
        success: function () {
            var $choice = $("#formid input[name='checkbox1']:checked"); //get the selected option 
            if ($choice.length) { // if an option is selected
                window.open('http://whitepaper.com/info/whitepaper/WhyBuy_WhitePaper_FinalB.pdf');
            } else {
                var $choice2 = $("#formid input[name = 'checkbox2']:checked "); //get the selected option 
                if ($choice2.length) { // if an option is selected
                    window.open('http://brochure.com/info/brochure/welcome-kit-brochure.pdf');
                }
            }
        },
        failure: function () {
            alert("
                There was an error processing your request.\nPlease
                try again.");
            return false;
        }
    });
});
    },
    failure: function () {
        alert("There was an error processing your request. \nPlease try again.");
        return false;
    }
});

});

于 2013-07-02T14:55:08.753 回答
0
`<input type="checkbox" name="checkboxG1m" id="checkbox_a" class="css-checkbox" />`
`$`("#checkbox_a").click(function(){
    if($("#checkbox_a").is(':checked')){
      window.open("x.pdf");
    }
});
于 2015-09-29T08:01:15.530 回答