2011-05-30 72 views
0

當我做這個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和Post,失敗的代碼。

+0

@Sandra:你的小提琴工作正常,我看到「555」出現在表格發佈之前... – 2011-05-30 13:40:30

+0

這適用於我 – wong2 2011-05-30 13:42:13

+0

你是對的。這裏發生了什麼!我一定已經簡化了這個問題。 http://jsfiddle.net/Cu5Mj/3/ – 2011-05-30 13:42:34

回答

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了一下。)

+1

這個問題在**中特別**,它是javascript中的一個保留字:https://developer.mozilla.org/en/JavaScript/Reference/Reserved_Words – 2011-05-30 15:11:11