2012-11-03 55 views
2

我正在嘗試使用g:link從Grails .gsp頁面調用jquery函數。這可能嗎?目前,我使用的是普通按鈕(這是成功的調用$("#opener")Grails g:調用jquery函數的鏈接

<li><button id="opener">Export</button></li> 

但我希望使用類似以下內容:

<div id="dialog" title="Export"> 
    <p>This is a sample dialog...</p> 
</div> 
<li><g:link class="export"> 
    <g:message code="default.new.label" args="[entityName]" /> 
</g:link></li> 

這裏是我的功能:

<link rel="stylesheet" 
href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" /> 
<script src="http://code.jquery.com/jquery-1.8.2.js"></script> 
<script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script> 
<style> 
    .ui-dialog .ui-dialog-buttonpane .ui-dialog-buttonset { text-align: center; float: none; } 
</style> 
<script> 

// increase the default animation speed to exaggerate the effect 
$.fx.speeds._default = 1000; 
$(function() { 
    $("#dialog").dialog({ 
     resizable: false, 
     autoOpen: false, 
     show: "blind", 
     hide: "explode", 
     buttons: { 
      "OK!": function() { 
       $(this).dialog("close"); 
      }, 
      Cancel: function() { 
       $(this).dialog("close"); 
      } 
     } 
    }); 

    $("#opener").click(function() { 
     $("#dialog").dialog("open"); 
     return false; 
    }); 
}); 
</script> 

謝謝!

回答

1

上次我需要d這個(Grails 1.3),我最終只使用了一個標準的HTML <a></a>連接onclick。例如:

<a href="#" class="export" onclick="some_function();"><g:message code="default.new.label" args="[entityName]" /></a> 

有可能是一種方式與一個Grails用的2.1.x現在的taglib要做到這一點,但如果有,我不知道這一點。

+0

我只是嘗試這樣做,而我收到錯誤「遺漏的類型錯誤:對象不是一個函數」。我上面發佈了我的功能。你能看到它有什麼問題嗎? – littleK

+0

你能否發佈你的相關GSP內容? –

+0

我加了一些相關的GSP內容,謝謝。 – littleK

2

在Grails的2.5,你可以這樣做:

<g:link onclick="alert('hello world')">