我使用(Css 2.1 和 Jquery)来设置文件输入的样式
一切都很好,现在一切都很好
这是我正在谈论的示例
如果您使用的是 Firefox,一切正常
但是使用 Chrome 时无法从第一次选择文件 我的问题
要在此演示中选择文件,它位于简历旁边
这是我的 JQuery 代码
(function($){
$.fn.SafyForm = function(){
$(".safy_input_file").live("click", function(){
var obj = $(this) ;
obj.prev("input[type=file]").click() ;
obj.prev("input[type=file]").change(function(){
var txt = $(this).val();
obj.children("span").text(txt) ;
});
}) ;
return this.each(function(){
if($(this).is("input[type=file]"))
{
$(this).hide().after('<div class="safy_input_file"><span> [ .docx & .doc & .pdf & .rar ] </span><label>اختار ملف</label></div>')
}
}) ;
}
})(jQuery);
$(document).ready(function(){
$('input[type=file]').SafyForm() ;
}) ;
更新后的代码
(function($){
$.fn.SafyForm = function(){
$(".safy_input_file").live("click",function(){
var obj = $(this) ;
obj.prev("input[type=file]").click() ;
}) ;
$("input[type=file]").change(function(){
var txt = $(this).val();
var safyobj = $(this).next('.safy_input_file') ;
safyobj.children("span").text(txt) ;
});
return this.each(function(){
if($(this).is("input[type=file]"))
{
$(this).hide().after('<div class="safy_input_file"><span> [ .docx & .doc & .pdf & .rar ] </span><label>اختار ملف</label></div>')
}
}) ;
}
})(jQuery);
$(document).ready(function(){
$('input[type=file]').SafyForm() ;
}) ;
最佳答案
你说得对,在chrome中运行了两次上传。 我在 fiddle 中测试了你的插件:
并且是正确的(运行 1 次)。 问题一定出在您的其他网页元素或脚本中。
也尝试改变:
input[type=file]
到
:file
关于javascript - jQuery:点击div后打开文件上传窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12802937/