javascript - 如何在 $.each ajax 中设置 href 值
问题描述
我正在一个线程内下载文件。我的问题是我无法在 href 和 download 属性中分配文件名,所以当我尝试下载它时,它不显示任何文件。我尝试用特定数据替换 href 并且它有效。
请帮我指定我在href和下载attrib中得到的文件的名称。
这是我的javascript:
$.each(data, function(key, value)
{
var message = value.message;
var cnt = value.count;
var n = message.includes('.jpg') || message.includes('.png') ||
message.includes('.jpeg')|| message.includes('.pdf') || message.includes('.csv') ||
message.includes('.docx') || message.includes('.xlsx');
message = value.message;
$('#divv').append('<a href="uploads/{{"'+value.message+'"}}"
download="{{"'+value.message+'"}}" style="font-weight: bold;"
class="bsi">' +
'<span class="glyphicon glyphicon-download"></span>'+
" "+value.message+'</a>'+
'<i class="bsi_date">'+value.dateReplied+" "+ value.username+'</i>');
getUserFullName(cnt);
}); //each
解决方案
您好,您可以使用此功能
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#?").attr("href", "https://whatyouwant/jquery/");
});
});
</script>
</head>
<body>
<p><a href="https://www.whatyouwant.com" id="?">whatyouwant.com</a></p>
<button>Change href Value</button>
<p>Mouse over the link (or click on it) to see that the value of the href attribute has changed.</p>
</body>
</html>
谢谢
推荐阅读
- asp.net-core - 如何在 ASP.NET core 2.1 项目中增加上传文件大小限制?
- java - 为什么我的 JFrame 在 Mac 上启动时会随机缩小?
- docker - Jsch命令执行挂起docker日志
- php - 有没有办法使用本地网络服务器从批处理文件运行 PHP 站点?
- apache-kafka-streams - Kafka Streams:动态配置 RocksDb
- sql-server - 通过应用程序角色在 Access to SQL Server 中创建链接表
- angular - 剑道网格导出到 Excel 保存到特定位置
- javascript - 如何发送js FormData
- velo - Wix - 不允许对只写数据集执行操作 (setFilter)
- javascript - 如何在 React Native 中使用 ref 清除 Text 组件