简体   繁体   English

隐藏/显示取决于输入类型=文件值

[英]Hide / show depending on input type=file value

It works fine when I select / deselect a file via the Choose File button:当我通过“选择文件”按钮选择/取消选择文件时,它工作正常:

 $("input[type='submit']").toggle(false); $("input[type='file']").change(function() { if ($(this).val() != "") { $("#btn").toggle(); } else { $("#btn").toggle(); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="fld" type="file" name="image" id="file"> <input id="btn" type="submit" value="Upload / Edit picture" name="upload_picture" id="upload_picture" class="btn" /> <br> <br> <div id="remove_button">Remove</div>

Click on the Choose File button and select a file.单击“选择文件”按钮并选择一个文件。 The Upload / Edit picture button will appear.将出现上传/编辑图片按钮。 Now click on the Choose File button again but do not select a file and exit the small popup window.现在再次单击“选择文件”按钮,但不要选择文件并退出小弹出窗口。 The Upload / Edit picture button will disappear.上传/编辑图片按钮将消失。

In my case I want the input="file" value to be cleared and the Upload / Edit picture button to disappear if I clear the value by clicking on the <div id="remove_button">Remove</div>在我的情况下,如果我通过单击<div id="remove_button">Remove</div>清除该值,我希望清除 input="file" 值并且上载/编辑图片按钮消失

Is there a way to do that?有没有办法做到这一点?

JSFiddle JSFiddle

What you can do is to bind a click event listener to the #remove_button element (which I recommend that you should be using <button> instead of a <div> ).您可以做的是将单击事件侦听器绑定到#remove_button元素(我建议您应该使用<button>而不是<div> )。

In the callback, you can simply set the value of the file input to an empty string, which effectively resets the field.在回调中,您可以简单地将文件输入的值设置为空字符串,从而有效地重置该字段。 Remember that you will need to use .trigger('change') to manually reinvoke the show/hide logic based on the file input value:请记住,您需要使用.trigger('change')根据文件输入值手动重新调用显示/隐藏逻辑:

 const $submit = $("input[type='submit']"); $submit.hide(); $("#fld").change(function() { $submit.toggle(!!this.value); }); $('#remove_button').click(function() { $('#fld').val('').trigger('change'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="fld" type="file" name="image" id="file"> <input id="btn" type="submit" value="Upload / Edit picture" name="upload_picture" id="upload_picture" class="btn" /> <br> <br> <button id="remove_button">Remove</button>

Add this code into your javascript将此代码添加到您的javascript中

$("#btn").toggle(false);

$("#fld").change(function(){

if($(this).val() != "")
{
 $("#btn").toggle();
} 
else{
 $("#btn").toggle();
}
});

$("#remove_button").click(function(){
  $("#fld").val('');
  document.getElementById("btn").style.visibility = "hidden";
})

and remove id tag from your html..it has two ids for the button change it like并从你的 html 中删除 id 标签..它有两个按钮的 id 改变它

<input id="btn" type="submit" value="Upload / Edit picture" name="upload_picture" 
class="btn" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM