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

时间:2014-03-25 04:15:35

标签: javascript grails gsp

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

<g:select id="plantselect"
name="plant" from="${plantList.list()}" 
value="${plant.id}" />

我不能像$ {plant}那样在视图上显示所选的值吗?

3 个答案:

答案 0 :(得分:0)

您可以使用以下代码显示下拉值:

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

答案 1 :(得分:0)

您应该将选定的ID简单地传递给gsp表单控制器(如果是另一个域值),然后使用

访问它 在您的jsp domain.thislistdomain.id中选择

Student.course.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标签,请参阅此处:

Grails remotefunction to populate dropdownlist with Jquery

答案 2 :(得分:0)

当然,试试这个:

<g:select id="plaintselect" name="plant.id" from="${Plant.list()}" optionKey="id" required="" value="${plant?.id}" class="many-to-one"/>

不要忘记在 Plant 类中覆盖 toString(),因为defolt使用此值来构建selectValue选项。

有关详细信息,请参阅http://grails.org/doc/latest/ref/Tags/select.html

相关问题