2012-03-16 86 views
0

我正在做一个文件上传的形式与下列按钮:“选择文件”,“上传”和“添加其他文件”

我试图删除属性“禁用”上传按钮时,文件输入更改)。它适用于第一个上传按钮,但不适用于第二个上传按钮等等...... 这是为什么?

非常感谢,如果你能帮助我。

我jQuery代码是:

$(document).ready(function() { 

$('input[type=file]').change(function(){ 
    $(this).next().removeAttr('disabled'); 
}); 

$('.add').click(function() { 
    var num = $('input[type=file]').length; 

    newNum = num + 1; 
    if (newNum >= 4){ 
     $('.add').hide(); 
     } 

    $('#addanother').append("<form method='POST'>"); 
    $('#photo1').clone().attr('id','photo'+newNum).appendTo('#addanother'); 
    $('#upload'+num).clone().attr('id','upload'+newNum).attr('disabled','disabled').appendTo('#addanother'); 
    $('#addanother').append('</form><br />'); 

    }); 
}); 

我的HTML代码是:

<form method='POST' enctype="multipart/form-data"> 
<input type='file' class='fileinput' id='photo1'> 

<input type='button' id='upload1' name='upload1' value='Upload' disabled='disabled'> 
</form> 

<div id='addanother'></div> 

<input type='button' class='add' id='add1' value='Add file'> 
+0

相反removeAttr看看在使用道具()http://api.jquery.com/prop/ – 2012-03-16 21:51:27

+0

你好周杰伦,我要找进去。非常感谢。我发现removeAttr的行为如此令人困惑。这种行为没有合乎逻辑的理由。 – eric01 2012-03-16 21:55:16

回答

0

要动态等事件处理程序没有义务加入第二上传按钮。使用.on在下面的语法,

$(document).on('change', 'input[type=file]', function(){ 
    $(this).next().removeAttr('disabled'); 
}); 

和删除,

$('input[type=file]').change(function(){ 
    $(this).next().removeAttr('disabled'); 
}); 
+0

是的,它确实工作,我不知道关于(),我正在读这个。感谢这一点,并让我知道接受的答案。之前,我曾经点击是的“这篇文章对你有用吗?”但我做错了! 此致敬礼 – eric01 2012-03-16 22:06:01

2

没有意识到你自己却在第一次修改DOM。

更换

$('input[type=file]').change(function(){ 
    $(this).next().removeAttr('disabled'); 
}); 

$(document).on({ 
    change: function() { 
     $(this).next().removeAttr('disabled'); 
    } 
}, 'input[type=file]'); 
+0

哦,我明白了,那是.on(events-map [,selector] [,data])语法。 +1 – 2012-03-16 22:00:27

+0

是的,它的作品,非常感谢!问候 – eric01 2012-03-16 22:03:58

相关问题