2012-03-16 33 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

相關問題