如果你還好用一個按鈕,而不是鏈接此使用G變得微不足道的:submitToRemote。但是,如果它是一個鏈接,你可以做一些醜陋這樣的:
的意見/郵件/ index.gsp中:
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sample title</title>
<g:javascript library="prototype" />
</head>
<body>
<g:form name="theForm">
Email: <g:textField name="emailAddr" />
<!-- Here comes the ugly -->
<a href="#" name="submit"
onclick="new Ajax.Updater('resp','${createLink(action:'addEmail')}',{asynchronous:true,evalScripts:true,parameters:Form.serialize(document.theForm)});return false">
Submit Form
</a>
</g:form>
<div id="resp">
</div>
</body>
</html>
電子郵件控制器:
class EmailController {
def index = { }
def addEmail = {
if(params?.emailAddr) {
render "${params.emailAddr}"
}
else {
render "No Email Entered"
}
}
}
夫婦注意事項如果你自定義: * Ajax.updater的第一個'resp'參數是你想要在Form.serialize(document.theForm)命令中更新 *的div的id,'theForm'需要以符合您分配表單的名稱。
這比我概述的方式更好;它允許你在不破壞實現的情況下從Prototype切換到其他東西。好建議,upvoted。 – proflux 2010-07-05 15:43:47
如果您將 var emailVal = $('email')。value; 這個函數裏面的這個函數真的很好。你能否在你的答案中做出改變,我會接受。謝謝。 – 2010-07-05 19:35:07
完成。 THX爲up proflux,希望有一個更優雅的方式來讓gsp和javascript進行交互,猜測它與服務器和其他客戶端之間的困難...... – Chris 2010-07-06 11:34:24