简体   繁体   English

使用JavaScript在groovy GSP中填充动态字段

[英]Populating Dynamic Fields in groovy GSP by using JavaScript

Hey Guys i am trying to populate dynamic fields in groovy GSP by using javascript and Groovy markup 大家好我正在尝试使用javascript和Groovy标记填充groovy GSP中的动态字段
*Here's the Effort 1 ->Works fine using plain html * *这是努力1->使用纯HTML可以正常工作*

<r:script>
  function createField()
  {
    var variable=0; 
    var d="<input type='text' name='item["+variable+"]' id='item["+variable+"]'/>";
    reutrn d;
  } 
</r:script>

*Here's the Effort 2 ->JavaScript Error while using Groovy tags * *这是使用Groovy标记时的工作2-> JavaScript错误*

<r:script>
  function createField()
  {
    var variable = 0; 
    var d = "<g:textField name='item["+variable+"]' id='item["+variable+"]' />";
    return d;
  }
</r:script>

By Using above function browser ends up with the error : javascript uncaught syntaxerror unexpected token illegal 通过使用上述功能,浏览器最终将出现以下错误: javascript uncaught语法错误意外令牌非法
So i decided to encode the tag using Grails InLine codec encodeAs="JavaScript" . 因此,我决定使用Grails InLine编解码器encodeAs =“ JavaScript”对标签进行编码。

*Here's the Effort 3 ->Error using Groovy tags * *这是努力3->使用Groovy标记时出错*

y<r:script>
   function createField()
   {
      var variable=0; 
      var d="<g:textField name='item["+variable+"]' id='item["+variable+"]' encodeAs="JavaScript"/>";
      return d;
   }
</r:script>

Problem with the third effort while encoding the tags, quotes besides the variable are also encoded so the output is something like u003b+variable+\', this makes the nonidentical form fields which are unable to process further. 编码标签时第三次尝试出现问题,除了变量外还引用了引号,因此输出类似于u003b + variable + \\ u0026#39,这使得无法进一步处理的表单字段不一致。
Any help will be appreciated. 任何帮助将不胜感激。

GSP and javascript are evaluated at different times which is causing this not to work. GSP和javascript的评估时间不同,这导致此方法无法正常工作。 GSP is executed server side then the result is sent back to the client the JS is executed client side. GSP在服务器端执行,然后将结果发送回客户端,JS在客户端执行。 If you view source of the page from your browser you will see that the createField function has the g:textField already replaced. 如果从浏览器查看页面源,您将看到createField函数已替换g:textField。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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