javascript - 使用javascript处理文件打开
问题描述
我正在调用一个函数 OpenFileHotlink(),它第一次工作正常,但是当我没有选择任何要打开的文件时,它正在打开以前的文件,如果没有选择的文件,我该如何处理它,那么它不应该打开。
我试图使用超时功能清除调用堆栈。
<fieldset>
<legend>File Name</legend>
<!----Here i am getting all files as dropdown and it changing on the basis of objectId(This is another dropdown option) and file types----->
<select id="hotlinkfilenames_Id" class="form-control">
<!-- <option value="">------Select------</option> -->
</select>
</fieldset>
<div class="col-md-12 text-center pt-10">
<a class="btn btn-success btn-xs" id="geturlOpenFileHotlink" onclick="OpenFileHotlink()">Open</a>
</div>
function OpenFileHotlink() {
var fileName = $("#hotlinkfilenames_Id").val();
var id=[];
var uri='';
alert(fileName);
if(fileName==null||fileName=='' ){
alert("Please select File Name");
//debugger;
return false;
}else{
alert("");
fileName=fileName.toString();
id =fileName.split("/");
var ext1=id[1].toString();
var ext=ext1.split(".");
var htmlOpenHotlink="";
$.ajax({
url:'./openFile1/'+id[0],
async: false,
cache: false,
type:'POST',
//dataType:'json',
success:function(response){
uri='data:image/'+ext[1]+';base64,'+response+'';
document.getElementById("geturlOpenFileHotlink").href = uri;
document.getElementById("geturlOpenFileHotlink").download = id[1];
}
});
}
}
解决方案
如果您只想清除输入字段,那么只需将 value 属性(或val()
在 jQuery 中)设置为空字符串即可。
在 ajax 请求的成功回调中,在末尾添加
$("#hotlinkfilenames_Id").val("");
根据相关更新进行编辑
我会给你简单的(香草)js方法。(在 jquery 旁边使用 js 绝对没问题。jquery 只是一个扩展普通 js 的库)。
document.getElementById('hotlinkfilenames_Id').innerHTML = '';
将此添加到您的成功方法中,它将清除选择下拉列表。希望能帮助到你。
推荐阅读
- axios - 在 nuxt-axios 响应拦截器中取消承诺
- c# - 集成 Azure Key Vault 时无法加载文件或程序集
- powershell - 批处理文件在执行下一个命令之前等待powershell行解压缩?
- javascript - 如何将嵌套数据解包到函数中?
- vue.js - Vuetify:数据表槽值切换另一个槽值
- javascript - 三 JS xhr Infinity % 已加载
- reactjs - 反应路由器没有呈现正确的组件
- sql-server - 从多个表中获取值
- gremlin - 通过 gremlin 服务器将多个 xml 数据文件插入到 tinkerpop 图形数据库中(使用 gremlin 代码)
- javascript - Puppeteer - 仅在存在时单击按钮