如何使用Javascript(或JQuery)浏览Grails中的关系
假设我们有一个具有下一个关系的域类如何使用Javascript(或JQuery)浏览Grails中的关系,javascript,jquery,grails,Javascript,Jquery,Grails,假设我们有一个具有下一个关系的域类Country: Town town 以及一个具有以下特征的域级城镇: Integer id String town select在以下代码行中运行良好(我使用的是Grails字段插件): 下一个代码不起作用(这是尝试使用Javascript浏览类): 我如何获得optionValue(或在关系中导航)以获得String town而不是Integer id?使用普通javascript 或者使用jQuery: 它使用.text获取值(但不使用.value)。
Country
:
Town town
以及一个具有以下特征的域级城镇:
Integer id
String town
select在以下代码行中运行良好(我使用的是Grails字段插件):
下一个代码不起作用(这是尝试使用Javascript浏览类):
我如何获得optionValue(或在关系中导航)以获得String town
而不是Integer id
?使用普通javascript
或者使用jQuery:
它使用
.text
获取值(但不使用.value
)。谢谢你,伙计!:)
<f:field bean="countryInstance" property="town" input-optionValue="town"/>
var townName= document.getElementById("town").value;
var townName= document.getElementById("town").options[document.getElementById("town").selectedIndex].value;
var townName= document.getElementById("town").town.value;
var el = document.getElementById("town");
// value
el.options[el.selectedIndex].value;
// text
el.options[el.selectedIndex].text;
$("#town option[value='2']").text()
$("#town option:selected").text()