How do I display the selected value of the drop down list on gsp?
<g:select id="plantselect"
name="plant" from="${plantList.list()}"
value="${plant.id}" />
Can't I to do something like ${plant} to display the selected value on view?
You can use the following code in order to display the drop down value:
<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>
you should pass that selected id simply to your gsp
form controller if it another domain value then access it using the
domain.thislistdomain.id
in your jsp Student.course.id
or some value selected and pass that id to the value of the g:select
:)
or on gsp itself you can do the following :
<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>
//or you can youse g:remote tag and please refer here :
Sure, try this:
<g:select id="plaintselect" name="plant.id" from="${Plant.list()}" optionKey="id" required="" value="${plant?.id}" class="many-to-one"/>
Don't forget to override toString() in Plant class, cos this value is used by defolt to build optionValue for select.
See http://grails.org/doc/latest/ref/Tags/select.html for more info
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.