我想以<input type="file">编程方式在标记上触发click事件.
只是调用click()似乎没有做任何事情,或者至少它没有弹出文件选择对话框.
我一直在尝试使用侦听器捕获事件并重定向事件,但是我无法像点击某个人那样实际执行事件.
我需要一个解决方案来在HTML中显示打开文件对话框,同时单击a div.div单击时,必须打开打开文件对话框.
我不想将输入文件框显示为HTML页面的一部分.它必须显示在单独的对话框中,该对话框不是网页的一部分.
我有一个包含一些数据和上传的表单.只有在成功接收和处理数据后才能启动上载.为此,我在我的地方进行ajax调用
click()的最后一件事不起作用,因为似乎异步调用块打开了一个上传窗口.它只有在我设置时才有效async: false.
我在文档和本网站中找不到任何内容,想知道那里有什么问题,以及如何让它保持调用异步?
例:
$.ajax({
type: "POST",
url: "/Save",
data: jsonText,
dataType: "json",
//async: false [1]
}).done(function (msg) {
$("#upload").click();
});
//$("#upload").click(); [2]
Run Code Online (Sandbox Code Playgroud)
演示:http://jsfiddle.net/c2v00uxn/
注意:
UPDATE
它不是关于如何一般地进行"点击",而是关于如何在异步ajax调用之后单击(截至目前,仅适用于非异步调用).
默认情况下,是否可以在其旁边没有输入的文件按钮?理想情况下,我想要的是一个按钮,让用户可以导航到文件而不显示他们在上传之前选择的内容.在用户选择文件后,我将通过以下内容提交表单:
$("#file").change(function() {
$("#update_button").trigger('click');
});
Run Code Online (Sandbox Code Playgroud)
我确信这一定是可能的,有些css或jquery魔法......
我有一个文本字段和按钮与以下css:
JS小提琴链接:http://jsfiddle.net/Tdkre/
.submit {
-moz-box-shadow:inset 0px 1px 0px 0px #cae3fc;
-webkit-box-shadow:inset 0px 1px 0px 0px #cae3fc;
box-shadow:inset 0px 1px 0px 0px #cae3fc;
background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #79bbff), color-stop(1, #4197ee) );
background:-moz-linear-gradient( center top, #79bbff 5%, #4197ee 100% );
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#79bbff', endColorstr='#4197ee');
background-color:#79bbff;
-moz-border-radius:6px;
-webkit-border-radius:6px;
border-radius:6px;
border:1px solid #469df5;
display:inline-block;
color:#ffffff;
font-family:arial;
font-size:14px;
font-weight:bold;
padding:5px 14px;
text-decoration:none;
text-shadow:1px 1px 0px #287ace;
cursor:pointer;
}
.submit:hover {
background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #4197ee), color-stop(1, #79bbff) ); …Run Code Online (Sandbox Code Playgroud) 我们希望减少用户在我们网站上传文件所需的步骤数; 所以我们使用jQuery使用下面的标记打开和回发文件(简化):
<a onclick="$('#uplRegistrationImage').click();">
Change profile picture
</a>
<!-- Hidden to keep the UI clean -->
<asp:FileUpload ID="uplRegistrationImage"
runat="server"
ClientIDMode="static"
Style="display:none"
onchange="$('#btnSubmitImage').click();" />
<asp:Button runat="server"
ID="btnSubmitImage"
ClientIDMode="static"
Style="display:none"
OnClick="btnSubmitImage_OnClick"
UseSubmitBehavior="False" />
Run Code Online (Sandbox Code Playgroud)
这在Firefox和Chrome中运行得非常好; 单击链接时打开文件对话框,并在选择文件时触发回发.
但是在文件上传加载并且用户选择了文件后的IE9中; 在OnChange工作的内容我得到"SCRIPT5访问被拒绝"错误.我已经尝试设置任意超时,设置间隔以检查文件是否无效.
还有其他一些与此有关的问题; 但是没有一个看起来有一个像样的答案(有人说将文件对话框设置为透明并悬停在按钮后面!)
有没有人解决这个问题?或者我是否必须为IE用户提供按钮?
我天真地尝试以下方法用JavaScript以编程方式打开文件选择器(请参阅此处的小提琴):
<input type='file'>?
<script>
$(function () {
$('input').click();
});
</script>
Run Code Online (Sandbox Code Playgroud)
以上不起作用.如何input type='file'用JavaScript 打开文件选择器?
我在javascript中创建一个上传控件,然后element.click()用于调出文件浏览器对话框.
function add(type) {
var element = document.createElement("input");
element.setAttribute("type", type);
element.setAttribute("value", type);
element.setAttribute("name", type);
element.setAttribute("id", "element-" + i);
var removebutton = document.createElement('a');
var removeimage = document.createElement('img');
removeimage.setAttribute("width", 15);
removeimage.setAttribute("height", 15);
removeimage.setAttribute("class", "removebutton");
removeimage.src = "/Content/Images/redx.png";
removebutton.appendChild(removeimage);
removebutton.setAttribute("id", "remove-" + i);
removebutton.setAttribute("onclick", "remove(" + i + "); return 0;");
var newfile = document.getElementById("uploadhere");
//newfile.appendChild(removebutton);
newfile.appendChild(element);
newfile.appendChild(removebutton);
element.click();
i++;
}
Run Code Online (Sandbox Code Playgroud)
文件broswer对话框按预期出现,但在我选择表单上的提交后,任何文件都输入到控件消息中.
如果我单击"浏览",我会收到文件broswer对话框,但文件上传正确.
如何将文件上载控件添加到我的表单,并让它显示文件broswer对话框,仍然按预期工作.
我想知道为什么这个例子在Chrome 10中有效,但在Fx 3.6中不起作用?IFAIK,确切地输入type ="file"点击不起作用...
谁能解释一下,为什么?
javascript ×8
html ×5
jquery ×4
css ×2
file ×2
asp.net ×1
c# ×1
click ×1
css3 ×1
dialog ×1
file-upload ×1
simulation ×1