【发布时间】:2014-05-24 14:39:16
【问题描述】:
我在前端和后端都使用了 handlebarsJS(使用 node.js),并使用 AJAX 将数据发布到 node.js API,它运行良好(通过 Postman 测试)
有没有办法在客户端手把模板脚本中提交表单?
现在,没有任何日志记录,并且在单击输入按钮时没有任何图形发生,否则它适用于新的 ajax 帖子,当然没有把手脚本..
这是 HTML/HandlebarsJS:
<div id="allStories" class="allStories"> </div><!--/allStories-->
<script id="storyTemplate" type="text/x-handlebars-template">
<div class="thisness">
<div class="stories">
<div class="new" id="new">
\{{#each stories}}
<div class="row moreTop1">
<div class="col-sm-4 col-sm-offset-4">
<form id="updateStoryForm">
<div class="input-group">
<span class="input-group-addon">
<input type="checkbox">
</span>
<input type="hidden" id="storyID" value="\{{ _id }}"/>
<input type="text" class="form-control" id="currentStory" value="\{{ story }}">
<span class="input-group-addon">
<input type="button" id="UpdateStory">
</span>
</div><!-- /input-group -->
</form>
</div><!-- /.col-lg-6 -->
</div><!-- /.row -->
\{{/each}}
</div>
</div> <!--/stories-->
</div> <!--/thisness-->
</script>
这里是 AJAX 更新:
$(document).ready(function(){
// UPDATE
// * * * * * * * * * * * * * * * * * * * * * * * * * * *
// UpdateStory button clicks
$( "#UpdateStory" ).click(function(e) {
e.preventDefault();
// story elements for API
var id = $( "#storyID" ).val();
var story = $( "#currentStory" ).val();
var datetimeNow = new Date();
if($("#archiveCheck").is(":checked")) {
archive = true;
} else {
archive = false;
}
console.log(id);
console.log(story);
console.log(datetimeNow);
console.log(archive);
var AjaxPostData = {
id : id,
story : story,
datetimeNow : datetimeNow,
archive : archive
};
console.log(AjaxPostData);
// if the story field has content
if (story.length != 0) {
console.log('there is a story: ' + story);
// make an ajax call
$.ajax({
dataType: 'json',
data: AjaxPostData,
type: 'post',
url:"http://localhost:4200/api/v1/stories" + id,
success: refreshNewStories,
error: foundAllNewFailure
});
};
console.log(AjaxPostData.id);
}); // UPDATE
}); // doc is ready
【问题讨论】:
-
从 ID 选择器更改为类选择器是否有效?由于您在 DOM 中循环,
#UpdateStoryid 不会是唯一的。尝试.UpdateStory并将其更改为一个类。
标签: jquery ajax forms templates handlebars.js