简体   繁体   中英

JavaScript not working in Grails/GSP

I have the following JavaScript code:

    <script type="text/javascript"><!--
​$(function () {
    $('#add').click(function() {
        $(this).before($('select:eq(0)').clone());
      if ($('select').length > 5) $(this).hide();        
    });
});​
//-->
</script>

Here is the HTML code:

<select name="dropdown">
    <option value="a">Apple</option>
    <option value="b">Bee</option>
    <option value="c">Cat</option>
    <option value="d">Donkey</option>
    <option value="e">Elephant</option>
</select>
<a href="#" id="add">Add</a>

In the jsfiddle demo its working fine, but its not working on my gsp. Any idea or I am missing out something? Whenever I click on the "Add" it just display /# on my url.

The error message I managed to figure out through inspecting the element is as follows: Uncaught SyntaxError: Unexpected token ILLEGAL

错误信息 Screenshot Chrome的控制台错误屏幕截图

Try uncommented version (w/o <!-- and //--> ):

<script type="text/javascript">
​$(function () {
  $('#add').click(function() {
     $(this).before($('select:eq(0)').clone());
     if ($('select').length > 5) $(this).hide();        
  });
});​
</script>

如果仅将JavaScript放在外部文件中,则可能可以避免此类问题。

There are probably some illegal characters in the <script> block.

Did you copy/paste it from somewhere else? I would suggest you put cursor at the start and end of each line and delete all characters till you are sure there are no non-visible characters.

Alternatively delete the whole script block and retype it manually

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