【发布时间】:2019-08-20 15:43:17
【问题描述】:
我发现了一些可以上传文件的 Javascript,它运行良好。但是,当我在 asp.net 页面上添加搜索时,它会触发一个清除文件的回发。这是一直触发 $(document).on("click", "#attachedfiles tr", function () 的行 我试过 "load","ready" 和 .one 回发总是触发 javascript。 应该使用什么来确保 javascript 仅在初始页面加载时触发?这是完整的脚本。
$(function () {
var fileExt = "";
var fileDesc = "";
fileExt = "*.jpg;*.jpeg;*.gif;";
fileDesc = "Allowed Files ";
fileExt += "*.mp3;*.wav";
fileExt += "*.flv;*.mpg;*.mp4;*.avi";
fileExt += "*.html,*.htm,*.doc;*.docx;*.pdf;*.rtf;*.txt,*.csv,*.xsl;*.xslx";
$("[id*=FileUpload1]").uploadify({
'swf': 'scripts/uploadify.swf',
'buttonText': 'Attach Files',
'uploader': 'uploadVB.ashx?key=<%=Key %>',
'method': 'post',
'fileSizeLimit': '12MB',
'fileTypeDesc': fileDesc,
'fileTypeExts': fileExt,
'multi': true,
'auto': true,
'onSelect': function (event, ID) {
$('#btnSendx0').prop('disabled', true);
$("#attachedfiles tr").each(function () {
if ($("td", this).eq(0).html() == event.name) {
alert(event.name + " already uploaded.");
$('#btnSendx0').prop('disabled', false);
$("#FileUpload1").uploadify('cancel');
return;
}
});
},
'onUploadComplete': function (event, ID, file, response, data) {
$('#btnSendx0').prop('disabled', false);
$("#attachedfiles").append("<tr class='someclass'><td>" + event.name + "</td><td><a href='javascript:;' >[x]</a></td></tr>");
}
});
});
$(document).on("click", "#attachedfiles tr", function () {
console.log($(this).attr('someclass'));
var row = $(this).closest("tr");
var fileName = $(this).closest('tr').find('td').eq(0).html();
var href = $(this).find("a").attr("href");
$.ajax({
type: "POST",
url: "sendEmails.aspx/RemoveFile",
data: '{fileName: "' + fileName + '", key: "<%=Key %>" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () { },
failure: function (response) {
alert(response.d);
}
});
row.remove();
});
【问题讨论】:
标签: javascript jquery asp.net