我是node.js和express的新手。我正在寫一個node.js應用程序,將express和句柄作爲我的模板框架。我想實現一個依賴選擇列表,它將根據另一個選擇列表的選定值進行呈現。在node.js + express中實現依賴選擇列表
到目前爲止,這是我:
這是我的父母選擇列表
<select id="buildingsSelect" class="building-select-add "name="residentBuildings">
{{#each buildingRows}}
<option value="{{idBuildings}}" name={{Name}}>{{Name}}</option>
{{/each}}
</select>
當用戶選擇一個建築,我做一個Ajax請求,
$('.building-select-add').change(function(){
var buildingId = $('.building-select-add').val();
$.ajax({
type: 'post',
url: '/resident_add/' + buildingId,
success: function(data) {
location.reload();
},
error: function(err) {
console.log('------------ Error while updating this resident: ' + err);
console.log('------------ Error while updating this resident: ' + err.message);
}
});
});
並將用於進行數據庫調用的值發送到Residents數據庫。
router.post('/:buildingId', function(req, res){
var buildingId = req.params.buildingId;
console.log('OOOOOO buildingId: ' + buildingId);
var resList = [];
connection.query('select idResidents, Name from Residents where BuildingId = ?', buildingId, function(err, rows){
if (err) {
console.log('----------------------- ERROR: Error querying records - ' + err);
console.log('----------------------- ERROR: ' + err.message);
return err;
}
resList = rows;
});
});
我想居民在另一個選擇顯示,但我無法填充它。
如何渲染頁面上的選擇?如果我在ajax中做了一個location.reload(),整個頁面都會被重新加載。有沒有辦法做到這一點?