当我这样做的时候
$(document).ready(function(){
$('form').live('submit', function(){
$('#template').tmpl([{ "id" : "555" }, { "in" : "checked" } ]).prependTo('#content');
});
});
使用和使用 HTML
<!DOCTYPE html>
<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.core.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.widget.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.datepicker.js"></script>
<script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>
<script type="text-x-jquery/template" id="template">
<form action="" method="post">
"${id}" <div class="cellData cellRadios"> <input name="ctype" value="individuel" type="radio" "${in}"/> </div>
</form>
</script>
</head>
<body>
<form action="" method="post">
<input value="Save" type="submit">
</form>
<br><br>
<div id="content"> </div>
然后 Firefox 中的错误控制台显示来自 JQuery.tmpl() 的 jquery.tmpl.min.js
第 1 行语法错误
JSFiddle 位于
是吗
$('#template').tmpl([{ "id" : "555" }, { "in" : "checked" } ]).prependTo('#content');
这是错误的吗?
更新更新了 JSFiddle 并发布了失败的代码。
最佳答案
我在您的 HTML 中更改了以下内容:
<script type="text/x-jquery-tmpl" id="template">
<form action="" method="post">
"${Id}" <div class="cellData cellRadios"> <input name="ctype" value="individuel" type="radio" ${In} /> </div>
</form>
</script>
和你的 JavaScript:
$(document).ready(function(){
$('form').live('submit', function(){
$('#template').tmpl({ "Id" : "555","In" : "checked" }).prependTo('#content');
return false;
});
});
现在它对我有用。
我认为问题出在模板变量名称上,我将它们大写,并且模板数据是一个由 2 个对象组成的数组,而不是一个简单的对象。 (还稍微更改了模板脚本 MIME。)
关于javascript - 这个 JQuery 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6177019/