Jquery $(“0”)[0]。单击();调用ajax后不工作
我会模拟点击下载一个名为ajax的文件来生成链接 Jquery:Jquery $(“0”)[0]。单击();调用ajax后不工作,jquery,ajax,click,Jquery,Ajax,Click,我会模拟点击下载一个名为ajax的文件来生成链接 Jquery: $(document).ready(function(){ $(".vignette-dl").click(function() { var id = $(this).find("a").attr("value"); $.ajax({ type: "POST", url: "testDL.php", data: { d
$(document).ready(function(){
$(".vignette-dl").click(function() {
var id = $(this).find("a").attr("value");
$.ajax({
type: "POST",
url: "testDL.php",
data: { dossier: $("#selectDossier").val(), id_doc: id },
success: function(result){
$("#secretDiv").html(result);
}
});
$(document).ajaxComplete(function(){
alert("test");
$("#forcedl")[0].click(function(){
alert("test42");
});
});
});
});
result var在secretDiv中添加了一个id=“forcedl”的html链接,效果非常好
调用ajaxComplete函数是因为我看到了警报测试,但是单击模拟没有工作,我没有看到警报测试42
我不知道为什么 jQuery.click(handler)
仅在单击元素时调用处理程序
$(document).ready(function(){
$(".vignette-dl").click(function() {
var id = $(this).find("a").attr("value");
$.ajax({
type: "POST",
url: "testDL.php",
data: { dossier: $("#selectDossier").val(), id_doc: id },
success: function(result){
$("#secretDiv").html(result);
$("#forcedl").click();
}
});
});
$(document).on('click', '#forcedl', function(e) {
e.preventDefault();
window.location.href = $(this).attr('href'); // use href-attribute or just replace with your path/to/file
})
});
为了下载PDF,您需要更改响应标题-这必须在服务器端完成。
请参见下面的.htaccess示例,它将使所有以.pdf结尾的文件直接下载
<FilesMatch "\.(?i:pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
ForceType应用程序/八位组流
标题集内容处置附件
有关php示例,请查看下面的代码示例
<?php
header('Content-type: application/pdf');
header('Content-disposition: attachment; filename=myNewFileName.pdf');
readfile("uri/to/my/file.pdf");
jQuery.click(handler)仅在单击元素时调用处理程序
$(document).ready(function(){
$(".vignette-dl").click(function() {
var id = $(this).find("a").attr("value");
$.ajax({
type: "POST",
url: "testDL.php",
data: { dossier: $("#selectDossier").val(), id_doc: id },
success: function(result){
$("#secretDiv").html(result);
$("#forcedl").click();
}
});
});
$(document).on('click', '#forcedl', function(e) {
e.preventDefault();
window.location.href = $(this).attr('href'); // use href-attribute or just replace with your path/to/file
})
});
为了下载PDF,您需要更改响应标题-这必须在服务器端完成。
请参见下面的.htaccess示例,它将使所有以.pdf结尾的文件直接下载
<FilesMatch "\.(?i:pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
ForceType应用程序/八位组流
标题集内容处置附件
有关php示例,请查看下面的代码示例
<?php
header('Content-type: application/pdf');
header('Content-disposition: attachment; filename=myNewFileName.pdf');
readfile("uri/to/my/file.pdf");
整个方法都是不合逻辑的,你不能有以下几点-
成功->点击
在解决方案中,创建一个设置为=0的变量
在ajax成功方法上,使变量=1
if === 0 {
// nothing here
}else{
// must ===1 something here
}
比其他人的想法快得多整个方法是不合逻辑的,你不能有以下几点-
成功->点击
在解决方案中,创建一个设置为=0的变量
在ajax成功方法上,使变量=1
if === 0 {
// nothing here
}else{
// must ===1 something here
}
比其他人的想法快得多我相信从你的描述来看,这就是我的愿望,但我做了一些假设
$(document).ready(function() {
$("#secretDiv").on('click', "#forcedl", function() {
alert("test42");
});
$(".vignette-dl").click(function() {
var id = $(this).find("a").attr("value");
$.ajax({
type: "POST",
url: "testDL.php",
data: {
dossier: $("#selectDossier").val(),
id_doc: id
}
}).done(function(result) {
$("#secretDiv").html(result);
}).complete(function() {
alert("test");
$("#forcedl").trigger('click');
});
});
});
我相信根据你的描述,这是我的愿望,但我做了一些假设
$(document).ready(function() {
$("#secretDiv").on('click', "#forcedl", function() {
alert("test42");
});
$(".vignette-dl").click(function() {
var id = $(this).find("a").attr("value");
$.ajax({
type: "POST",
url: "testDL.php",
data: {
dossier: $("#selectDossier").val(),
id_doc: id
}
}).done(function(result) {
$("#secretDiv").html(result);
}).complete(function() {
alert("test");
$("#forcedl").trigger('click');
});
});
});
为什么使用[0]?您正在尝试附加事件处理程序,而不是触发event@RoryMcCrossan你应该加上这个作为答案。同样,请参见文档:$(“#forcedl”).trigger('click')
或$(“#forcedl”)。在('click',function(){alert(“test42”)})上代码>不确定您正在尝试什么,但最后一个会在每个ajaxComplete上添加新的事件处理程序,这太不好了。放置警报($(“#forcedl”).length)的结果是什么代码>位于ajaxComplete函数的顶部?还有,在success函数中,alert(JSON.stringify(result))代码>生成?为什么使用[0]?您正在尝试附加事件处理程序,而不是触发event@RoryMcCrossan你应该加上这个作为答案。同样,请参见文档:$(“#forcedl”).trigger('click')
或$(“#forcedl”)。在('click',function(){alert(“test42”)})上代码>不确定您正在尝试什么,但最后一个会在每个ajaxComplete上添加新的事件处理程序,这太不好了。放置警报($(“#forcedl”).length)的结果是什么代码>位于ajaxComplete函数的顶部?还有,在success函数中,alert(JSON.stringify(result))代码>生成?这与我的代码相同,触发器('click')不起作用:/这与我的代码相同,触发器('click')不起作用:/我想你的问题是不同的:。click()只模拟JavaScript事件,而不是实际单击元素。尝试用window.location.href='/path/to/my/file'替换它代码>我编辑了我的答案。不再工作,我尝试了chrome和ie11。。secretDiv中id=“forcedl”的html链接添加了dinamiccaly,工作正常,但无法下载。我尝试在e.preventDefault()之后添加一个警报,但它没有出现,因此您在ajax调用中插入了#forcedl元素。检查我的更新答案。好吧,这样更好,pdf会出现在网页上:)但是我需要强制下载而不查看itI我想你的问题是另一个:。click()只模拟JavaScript事件,而不是实际单击元素。尝试用window.location.href='/path/to/my/file'替换它代码>我编辑了我的答案。不再工作,我尝试了chrome和ie11。。secretDiv中id=“forcedl”的html链接添加了dinamiccaly,工作正常,但无法下载。我尝试在e.preventDefault()之后添加一个警报,但它没有出现,因此您在ajax调用中插入了#forcedl元素。检查我的更新答案。好的,这样更好,pdf会出现在网页上:),但我需要强制下载而不查看它