我試圖從postgresql顯示數據到slickgrid。但是,在javascript for循環中,我無法從JSP表達式中檢索正確的值。 我該怎麼辦?javascript變量到jsp表達式
conn = dataSource.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT host_name,username,key_value,updated from pass_table");
while(rs.next()) {
hostname.add(rs.getString(1));
}
} catch (Exception e) {
out.println("<br /><font color='red'><strong>SQL Exception: " + e + "</strong></font><br/>");
}
%>
$(function() {
var data = [];
for (var i = 0; i < 2; i++) {
data[i] = {
//title: "Task " + i
title: "<%= hostname.get(i)%>" <===this part
//title: "${row.host_name}" + i
};
}
grid = new Slick.Grid("#myGrid", data, columns, options);
})
</script>
這個問題可能重複檢查出這個[JSP-JS(http://stackoverflow.com/questions/4803906/reading-a-jsp-variable-from-javascript)我認爲你有問題在這個'<%= hostname.get(i)%>'代碼中使用'i'。下面 – Hashy