您的 select 标记不必位于 formRemote 标记内,带有 remoteFunction 调用的常规表单标记就可以了。
来自http://grails.org/AJAX-Driven+SELECTs+in+GSP:
在 gsp 中选择标签:
<form>
<g:select
optionKey="id" optionValue="name" name="country.name" id="country.name" from="${Country.list()}"
onchange="${remoteFunction(
controller:'country',
action:'ajaxGetCities',
params:'\\'id=\\' + escape(this.value)',
onComplete:'updateCity(e)')}"
></g:select>
<g:select name="city" id="city"></g:select>
</form>
gsp 中的 javascript 函数将数据发送到 grails 控制器:
<g:javascript>
function updateCity(e) {
// The response comes back as a bunch-o-JSON
var cities = eval("(" + e.responseText + ")") // evaluate JSON
if (cities) {
var rselect = document.getElementById('city')
// Clear all previous options
var l = rselect.length
while (l > 0) {
l--
rselect.remove(l)
}
// Rebuild the select
for (var i=0; i < cities.length; i++) {
var city = cities[i]
var opt = document.createElement('option');
opt.text = city.name
opt.value = city.id
try {
rselect.add(opt, null) // standards compliant; doesn't work in IE
}
catch(ex) {
rselect.add(opt) // IE only
}
}
}
}
// This is called when the page loads to initialize city
var zselect = document.getElementById('country.name')
var zopt = zselect.options[zselect.selectedIndex]
${remoteFunction(controller:"country", action:"ajaxGetCities", params:"'id=' + zopt.value", onComplete:"updateCity(e)")}
</g:javascript>
圣杯控制器:
import grails.converters.*
class CountryController {
def ajaxGetCities = {
def country = Country.get(params.id)
render country?.cities as JSON
}
}