2013-07-02 39 views
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> 

任何帮助表示赞赏。

回答

1

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

事情的关键在这里:AJAX调用是异步,所以AJAX请求被解雇,而不是之后响应到达之后的PDF将被打开。 类似这样的:

$("#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; 
    } 
}); 

});

+0

感谢您的快速回复。除了最初的成功参数需要在那里的formReturn之外,这种方法才有效。我应该包含JavaScript验证,但它基本上处理所有的表单域。无论如何要把它包含在PDF文件中吗?再次感谢。 – UTvolfan85

+0

在PDF文件中包含什么? html表单验证? –

+0

不,包含AJAX Post的Success部分中的formReturn参数以及PDF下载。 – UTvolfan85

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"); 
    } 
}); 
相关问题