[英]How do I display the selected value of the drop down list on gsp
如何在gsp上顯示下拉列表的選定值?
<g:select id="plantselect"
name="plant" from="${plantList.list()}"
value="${plant.id}" />
我不能做類似$ {plant}的操作來在視圖上顯示選定的值嗎?
您可以使用以下代碼來顯示下拉值:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sample title</title>
<script>
function getValue(value)
{
alert(value);
document.getElementById("test").style.display = "inline";
document.getElementById("test").innerHTML = value;
// $("#test").html(value);
}
</script>
</head>
<body>
<h1>Sample line</h1>
<g:select id="plantselect"
name="plant" from="${plantList.list()}"
optionKey="id" optionValue="id" onchange="getValue(this.value)"/>
<!-- if you want to other field than id then change id to that field.-->
<br/>
<div id="test" style="display: none">
</div>
</body>
</html>
您應該將所選的ID僅傳遞給gsp
表單控制器(如果它是另一個域值),然后使用
您的jsp Student.course.id
domain.thislistdomain.id
或選定的某個值,然后將該ID傳遞給g:select的值
:)
或在gsp本身上,您可以執行以下操作:
<g:javascript>
$( "gselectidhere" ).change(function() {
alert( "You selected"+this.val());
//if you want to process and communicate at selection with the controller please add ajax post or get call here ...
});
</g:javascript>
//或者您可以使用g:remote標記,請在這里參考:
當然,請嘗試以下操作:
<g:select id="plaintselect" name="plant.id" from="${Plant.list()}" optionKey="id" required="" value="${plant?.id}" class="many-to-one"/>
不要忘記在Plant類中重寫toString() ,因為該值由defolt用來構建select的optionValue。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.