如果我点击按钮标签,我希望能够打开图像上传文件对话框。这是否可能?如果是,我该如何在 PHP 中实现它?
while{
echo "<td><button><img src='".$cfet['productimage']."' width='50' height='40'></button></td>";
}
如果我点击按钮标签,我希望能够打开图像上传文件对话框。这是否可能?如果是,我该如何在 PHP 中实现它?
while{
echo "<td><button><img src='".$cfet['productimage']."' width='50' height='40'></button></td>";
}
在你的HTML页面上包含一个输入类型为“file”的元素,在按钮的点击事件中使用jQuery的trigger函数触发文件输入类型元素的点击事件。
代码将如下所示:
<input type="file" id="imgupload" style="display:none"/>
<button id="OpenImgUpload">Image Upload</button>
在按钮的点击事件中编写 jQuery 代码,如下:
$('#OpenImgUpload').click(function(){ $('#imgupload').trigger('click'); });
这会在您的按钮单击事件上打开文件上传对话框。
<input type="file" id="imgupload" style="display:none"/>
<label for='imgupload'> <button id="OpenImgUpload">Image Upload</button></label>
点击for=属性会自动聚焦于“文件输入”并打开上传对话框。
你需要加入一点小技巧来实现这个。
你可以将文件上传(input type=file
)隐藏在你的 button
后面。
并且在你的按钮上点击时,你可以触发你的文件上传点击。
这样会在按钮点击时打开一个文件上传窗口。
<button id="btnfile">
<img src='".$cfet['productimage']."' width='50' height='40'>
</button>
<div class="wrapper"> //set wrapper `display:hidden`
<input type="file" id="uploadfile" />
</div>
以及一些 JavaScript
$("#btnfile").click(function () {
$("#uploadfile").click();
});
这个例子的 JSFiddle 链接为:http://jsfiddle.net/ravi441988/QmyHV/1/embedded/result/
<input type="file" id="imgupload">
<a href="#" onclick="$('#imgupload').trigger('click'); return false;">Upload file</a>
<input type="file" name="pic1" id="pic1" style="display:none;"/>
<label for="pic1">
<img src="dist/img/picfilename.png">
</label>
input
移动到label
内部,并且不需要使用for/id
属性进行关联 - 这样做非常有效。 - Ragen Dazs <label for="profileImage">
<a style="cursor: pointer;"><em class="fa fa-upload"></em> Change Profile
Image</a></label>
<input type="file" name="profileImage" id="profileImage" style="display: none;">
您可以通过onclick函数显示文件选择对话框,如果选择了一个文件(onchange事件),则发送表单以上传文件。
<form id='foto' method='post' action='upload' method="POST" enctype="multipart/form-data" >
<div style="height:0px;overflow:hidden">
<input type="file" id="fileInput" name="fileInput" onchange="this.form.submit()"/>
</div>
<i class='fa fa-camera' onclick="fileInput.click();"></i>
</form>
<button id="OpenImgUpload" onclick="$('#imgupload').trigger('click');">Image Upload</button>
<input type="file" id="imgupload" style="display:none"/>
<!-- File input (hidden) -->
<input type="file" id="file1" style="display:none"/>
<!-- Trigger button -->
<a href="javascript:void(0)" onClick="openSelect('#file1')">
<script type="text/javascript">
function openSelect(file)
{
$(file).trigger('click');
}
</script>
<!--Hide input field to open it using button-->
<input type="file" #file11 style="display:none">
and <button type="file" (click)="file11.click()">Open file dialog</button>