这个JQuery有什么问题?

时间:2011-05-30 13:38:13

标签: javascript jquery

当我这样做时

$(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.min.js的第1行中显示语法错误,该错误来自JQuery.tmpl()

JSFiddle

http://jsfiddle.net/Cu5Mj/4/

是吗

$('#template').tmpl([{ "id" : "555" }, { "in" : "checked" }   ]).prependTo('#content');

那是错的?

更新更新了JSFiddle并发布了失败的代码。

1 个答案:

答案 0 :(得分:2)

我更改了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。)

相关问题