简体   繁体   中英

What's wrong with this JQuery?

When I do

$(document).ready(function(){
   $('form').live('submit', function(){
      $('#template').tmpl([{ "id" : "555" }, { "in" : "checked" }   ]).prependTo('#content');
   });
});

with and with 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>

then Error Console in Firefox says Syntax Error in line 1 of jquery.tmpl.min.js which is from JQuery.tmpl()

JSFiddle at

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

Is it

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

that is wrong?

Update Updated JSFiddle and post with code that fails.

I changed the followings in your 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>

and your JavaScript:

$(document).ready(function(){
   $('form').live('submit', function(){
      $('#template').tmpl({ "Id" : "555","In" : "checked" }).prependTo('#content');
       return false;
   });
});

and it works for me now.

The problems I think was the template variable names, I capitalized them, and the template data was an array of 2 objects instead of a simple object. (Also changed the template script MIME a bit.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM