0

I have a page below which is using jquery to append some html. It simply append an input tag in the specified DIV

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script src="../../jquery-1.10.2.min.js" type="text/javascript" xml:space="preserve"></script>
    <script type="text/javascript" xml:space="preserve">
    var i = 0;
    function test(){
        var totalQuantity = '<input type="number" name="order' + i + '.totalQuantity" />';
        $(".test").append(totalQuantity);
        i++;
    }
    </script>
  </head>
  <body>
    <div class="test"></div>
    <input type="button" onclick="test()" value="Add" />
  </body>
</html>

When I run this on server in tomcat using spring, on inspect element the javascript becomes something like:

var totalQuantity = '<input type="number" name="order&#39;+i+ &#39;.totalQuantity" />';

All the ( ' ) characters replaced by code (#&39).

what is the problem actually? Why it is not showing character ( ' ) but rather showing its html code?

Is this a problem with using spring because when I separately(not in a container or spring framework) open this html file, it works fine.

4

1 回答 1

0

While this is a workaround, i think it's the proper way to insert javascript into html.

Move your javascript into an external file so that whatever is pre-processing your HTML won't touch your javascript.

于 2013-10-07T15:41:50.303 回答