2012-11-03 52 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')">