這是一個JavaScript函數,我必須根據onload期間文本文件中的內容動態創建按鈕。雖然它能夠讀取文件,但我使用警報消息來檢查var按鈕是否正確,但似乎無法創建按鈕。Ajax/Javascript動態創建按鈕的問題
function createButtons(){
$(document).ready(function() {
alert("1");
$.ajax({
url : 'http://localhost:8080/SSAD/type.txt',
dataType : "text",
success : function (filecontent) {
var lines=filecontent.split('\n');
$.each(lines, function() {
if (this!='') {
var button='<button type="button" class="btn btn-block btn-inverse active" data-toggle="button tooltip" title="Click this to enable/disable viewing of'+this+'" onclick="toggleVisibility('+"'"+this+"'"+')">'+this+'</button>';
$("#crisisButtons").append(button);
}
});
}
});
});
}
HTML:
<div class="crisisButtons"></div>
<script type="text/javascript">window.onload = createButtons();</script>
你確定了'成功'功能火災? –
很確定它確實如我在if循環中添加了一個警告(this),它顯示了文本文件中的內容。 –