首页 > 解决方案 > 如何在 $.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

标签: javascriptjquery

解决方案


您好,您可以使用此功能


<!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>

谢谢


推荐阅读