jquery 未正确更新车把局部视图(表示,node.js)
jquery not updating handlebars partial view correctly (express, node.js)
已更新以展示如何让它工作。我在下面接受的答案并没有真正显示太多代码,所有内容都在评论中。因此,为简单起见,我在这里显示了正确的代码(我的原始代码有 3 个错误)。
我将 Node.JS 与 Express 和 Handlebars 一起使用。我在更新特定 div 以及发送数据时遇到了麻烦。这就是有效的方法。
在我的视图目录中,我有:index.handlebars、view1.handlebars、temp.handlebars。
在view1.handlebars里面,我有一个button
:
<button type="button" id="btn-here" data-id="{{data.uniqueId}}"><a href="../update_this">Update This</a></button>
和一个div
:
<div id="sectional">
{{> partial}}
</div>
当有人点击 button
时,我想更新部分 div
。所以我像这样使用 jquery:
$('#btn-here').on('click', function(event) {
event.preventDefault();
var id_num = $(this).data('id'); //id_num is correct
$.get("/temp/" + id_num, function(res) {
var updated_html = res;
$('#sectional').html(updated_html);
});
});
我允许 jquery 通过以下方式找到 temp.handlebars 文件:
router.get('/temp/:id', view1.showstuff);
在 view1.showstuff 函数中:
showstuff: function(req, res) {
res.locals.model = model;
res.render('temp/' + id, {layout: false});
};
这现在会发送视图数据并仅更新在 jquery 调用中指定的视图。
感谢下方用户的帮助!
您要么需要如下的路由器处理程序
app.get('/temp.html', function (req, res, next) {
res.render('temp', {layout: false});
});
或者将您的 temp.html 放在 public 文件夹中,但是您不能使用 {{> partials}} 因为它没有被视图引擎处理
已更新以展示如何让它工作。我在下面接受的答案并没有真正显示太多代码,所有内容都在评论中。因此,为简单起见,我在这里显示了正确的代码(我的原始代码有 3 个错误)。
我将 Node.JS 与 Express 和 Handlebars 一起使用。我在更新特定 div 以及发送数据时遇到了麻烦。这就是有效的方法。
在我的视图目录中,我有:index.handlebars、view1.handlebars、temp.handlebars。
在view1.handlebars里面,我有一个button
:
<button type="button" id="btn-here" data-id="{{data.uniqueId}}"><a href="../update_this">Update This</a></button>
和一个div
:
<div id="sectional">
{{> partial}}
</div>
当有人点击 button
时,我想更新部分 div
。所以我像这样使用 jquery:
$('#btn-here').on('click', function(event) {
event.preventDefault();
var id_num = $(this).data('id'); //id_num is correct
$.get("/temp/" + id_num, function(res) {
var updated_html = res;
$('#sectional').html(updated_html);
});
});
我允许 jquery 通过以下方式找到 temp.handlebars 文件:
router.get('/temp/:id', view1.showstuff);
在 view1.showstuff 函数中:
showstuff: function(req, res) {
res.locals.model = model;
res.render('temp/' + id, {layout: false});
};
这现在会发送视图数据并仅更新在 jquery 调用中指定的视图。
感谢下方用户的帮助!
您要么需要如下的路由器处理程序
app.get('/temp.html', function (req, res, next) {
res.render('temp', {layout: false});
});
或者将您的 temp.html 放在 public 文件夹中,但是您不能使用 {{> partials}} 因为它没有被视图引擎处理