什么'这个JQuery有问题

What's wrong with this JQuery?

本文关键字:JQuery 有问题 这个 什么      更新时间:2023-09-26

当我进行时

$(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并发布失败的代码。

我在您的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。)