Javascript 如何在gsp上显示下拉列表的选定值

Javascript 如何在gsp上显示下拉列表的选定值,javascript,grails,gsp,Javascript,Grails,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;

如何在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>

示例标题
函数getValue(值)
{
警报(值);
document.getElementById(“test”).style.display=“inline”;
document.getElementById(“test”).innerHTML=value;
//$(“#测试”).html(值);
}
采样线


如果它是另一个域值,则应将所选id简单地传递给您的
gsp
表单控制器,然后使用

domain.thislistdomain.id
在jsp
Student.course.id
中选择某个值,并将该id传递给g:select的值

:)

或者在普惠制本身上,您可以执行以下操作:

<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>

$(“gselectidhere”).change(函数(){
警报(“您选择了”+this.val());
//如果您想在选择时与控制器进行处理和通信,请在此处添加ajax post或get call。。。
});
//或者您可以使用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


有关更多信息,请参见

我想在将所选值传递给控制器之前进行检查。我正在尝试检查所选值,并执行“如果其他”!所以它基本上在同一页上。假设我想更改标签名称,如果选择的值是x…@嘿,这很容易,在更改时使用jquery函数,然后执行普通的java脚本,如果然后更改标签…我已经回答了你的问题~!是否要在同一gsp上显示下拉列表的选择值?还是要在任何字段上显示下拉列表的选定值?