我正在寫一個簡單的小JavaScript,並且每次點擊一個按鈕時基本上都會附加一個元素。我也想限制元素可以通過計數器和條件語句附加的次數。JavaScript增量變量無法有條件地讀取
驗證碼:
HTML
<p id="moreUpload">click me</p>
<div id="file-uploads"></div>
的JavaScript
var div = "<div class='fileupload fileupload-new' data-provides='fileupload'><span class='btn btn-file'><span class='fileupload-new'>Select file</span><span class='fileupload-exists'>Change</span><input type='file' name='file[]' /></span><span class='fileupload-preview'></span><a href='#' class='close fileupload-exists' data-dismiss='fileupload' style='float: none;'>x</a></div>";
var count = 1;
if (count < 6) {
$("#moreUpload").click(function() {
$(div).hide().appendTo("#file-uploads").fadeIn(500);
alert(count);
count++;
});
}
基本上是以書面的JavaSc ript將允許無限附加,我認爲增量不起作用。然而,警報呼出了我期望看到的數字。我究竟做錯了什麼?