如何在Javascript中传递或使用GSP中的变量

如何在Javascript中传递或使用GSP中的变量,javascript,grails,gsp,Javascript,Grails,Gsp,我的grails gsp文件中有以下行 <div class="pagination"> <g:paginate total="${lotCount ?: 0}" /> </div> 但它不起作用。 获取了类似ReferenceError的错误:未定义ID。 请提供帮助。使用隐藏字段: <g:hiddenField name="lotCount" value="${lotCount}" /> <div cl

我的grails gsp文件中有以下行

    <div class="pagination">
        <g:paginate total="${lotCount ?: 0}" />
    </div>
但它不起作用。 获取了类似ReferenceError的错误:未定义ID。 请提供帮助。

使用隐藏字段:

<g:hiddenField name="lotCount" value="${lotCount}" />

<div class="pagination">
    <g:paginate total="${lotCount ?: 0}" />
</div>

您的解决方案应该有效

只需确保在源代码中生成html页面行

<g:javascript> var theId = ${lotCount} </g:javascript>
  • 如另一条评论所述,通过使用隐藏字段:
  • 普惠制:


    首先,在id调用周围放置语音标记,以便
    var theId=“${lotCount}”@vahid I仍然存在与未定义ID相同的错误
    
    <g:hiddenField name="lotCount" value="${lotCount}" />
    
    <div class="pagination">
        <g:paginate total="${lotCount ?: 0}" />
    </div>
    
    <g:javascript> var theId = ${lotCount} </g:javascript>
    
    <script type="text/javascript" src="area-switcher.js">
    
    <div id="countElem" data-count="${count}">
    
    var count = $("#countElem").data('count');
    
    <g:hiddenField name="countElem" data-count="${count}"/>
    
    // hidden field will have name and id equals to countElem
    var count =  $("#countElem").val();