如何从网页表单上的文本框中获取输入 [英] How to get the input from a text box on a webpage form

查看:100
本文介绍了如何从网页表单上的文本框中获取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Grails上的!
这是一个非常基本的东西,我无法理解。



我在index.gsp中有这个

 < g:form name =testFormurl =[controller:'test',action:'index']> 
< g:textField name =Input Avalue =$ {Input1}> < /克:文本字段>
< / g:表格>

在我的TestController类中也有这个:

  class TestController {

def index = {
def Input1
def Input2
}
}

我想获得用户在网页上输入的两个输入并将它们保存到相应的字段(输入1,输入2)在控制器上。



我该如何处理它?<​​b>

谢谢

解决方案

您可以像这样编写表单:

 < g:form name =testFormcontroller =testaction =index> 
< g:textField name =Input1value =$ {Input1}> < /克:文本字段>
< / g:表格>

请注意,在这种情况下,


  1. 不需要g:form标签的控制器参数,它可以按照惯例使用
  2. 根据您的路线(和grails版本) ),但大多数情况下,这是你指定的表单,所以Grails知道要提交的内容。
  3. 参数稍微不同步Input A - >Input1 li>

然后在控制器中

  class TestController {
$ b def index = {
def Input1 = params.Input1
def Input2 = params.Input2
[Input1:Input1,Input2:Input2 ]
}
}

有了这个值,在返回的模型中)


This is on Grails! This is a very basic thing which apperatnly I am failing to understand.

I have this in my index.gsp

<g:form name="testForm" url="[controller:'test',action:'index']">
   <g:textField name="Input A" value="${Input1}">  </g:textField>
   <g:textField name="Input B" value="${Input2}"> </g:textField> 
</g:form>

I also have this in my TestController class:

class TestController {

    def index = {
        def Input1
        def Input2
    }
}

I want to get the two inputs that the user enters on the webpage and save them to the appropriate fields (Input1, Input2) on the controller.

How do I go about it?

thanks

解决方案

You can write your form like this:

<g:form name="testForm" controller="test" action="index">
 <g:textField name="Input1" value="${Input1}">  </g:textField>
 <g:textField name="Input2" value="${Input2}"> </g:textField> 
 <g:actionSubmit value="Send to controller"  action="index"/>
</g:form>

Note that in this case,

  1. the controller parameter for the g:form tag is not needed, it is used by convention
  2. Action may also probably be removed depending on your route (and grails version), but most of the time, this is what you specify to the form so grails knows where to submit
  3. Parameters were slightly out of sync "Input A" -> "Input1"

Then in the controller

class TestController {

   def index = {
    def Input1 = params.Input1
    def Input2 = params.Input2
    ["Input1": Input1, "Input2": Input2]
   }
 }

With this, the values will be rendered properly (inside the returned model)

这篇关于如何从网页表单上的文本框中获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆