Grails 中的 Ajax 驱动选择不刷新选择
我已经使用 Grails 站点示例 代码实现了 ajax 选择,其中用户选择一个国家/地区,城市字段就会更新。我使用 jQuery 而不是 Prototype。
选择新国家/地区后,城市字段不会更改,但当用户单击城市选择时,它仅显示新城市,用户可以选择其中之一。
城市选择列表正在更新,但当前显示的值不会自动更新。
这是视图:
<div data-role="fieldcontain">
<label for="country">Country</label>
<g:select
optionKey="id" optionValue="name" name="country" id="country"
from="${com.TourneyCard.Country.list(sort:'name')}" value="${homeCountry.id}"
onchange="${remoteFunction(
action:'ajaxGetCityJSON',
params:'\'id=\' + escape(this.value)',
onSuccess:'updateCity(data);')}">
</g:select>
</div>
<div data-role="fieldcontain">
<label class="ui-select" for="city">City</label>
<g:select name="city" id="tee" data-native-menu="true"
optionKey="id" optionValue="name" from="${homeCities}">
</g:select>
</div>
这是 javascript:
<g:javascript>
function updateCity(data) {
if (data) {
var rselect = document.getElementById('city')
var l = rselect.length
while (l > 0) {
l--
rselect.remove(l)
}
for (var i = 0; i < data.length; i++) {
var tee = data[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
}
}
}
}
window.onload = function() {
var zselect = document.getElementById('city')
var zopt = zselect.options[zselect.selectedIndex]
${remoteFunction(
action: "ajaxGetCityJSON",
params: "'id=' + zopt.value",
onSuccess: "updateCity(data)")}
}
</g:javascript>
I've implemented an ajax select using the Grails site example code where the user selects a country and the city field is updated. I'm using jQuery instead of Prototype.
After selecting a new country, the city field does not change but when the user clicks on the the city select, it shows only the new cities and the user can select one of them.
The city select list is being updated but the currently displayed value is not automatically updated.
This is the view:
<div data-role="fieldcontain">
<label for="country">Country</label>
<g:select
optionKey="id" optionValue="name" name="country" id="country"
from="${com.TourneyCard.Country.list(sort:'name')}" value="${homeCountry.id}"
onchange="${remoteFunction(
action:'ajaxGetCityJSON',
params:'\'id=\' + escape(this.value)',
onSuccess:'updateCity(data);')}">
</g:select>
</div>
<div data-role="fieldcontain">
<label class="ui-select" for="city">City</label>
<g:select name="city" id="tee" data-native-menu="true"
optionKey="id" optionValue="name" from="${homeCities}">
</g:select>
</div>
Here is the javascript:
<g:javascript>
function updateCity(data) {
if (data) {
var rselect = document.getElementById('city')
var l = rselect.length
while (l > 0) {
l--
rselect.remove(l)
}
for (var i = 0; i < data.length; i++) {
var tee = data[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
}
}
}
}
window.onload = function() {
var zselect = document.getElementById('city')
var zopt = zselect.options[zselect.selectedIndex]
${remoteFunction(
action: "ajaxGetCityJSON",
params: "'id=' + zopt.value",
onSuccess: "updateCity(data)")}
}
</g:javascript>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将新选项添加到城市选择列表后,添加以下行。
这应该重置列表中的选定选项,并强制浏览器重新显示列表而无需用户干预。
Add the following line after you have added in the new options to the city select list.
This should reset the selected option within the list and force the browser to re-display the list without user intervention.