0

我对 Web 开发很陌生,并且遇到了一些语法问题。我有两个textarea。用户在 中输入信息后textarea,他/她将按下execute-button,它应该将这两个数据传递textarea给 acontroller进行一些数据处理。我正在使用 Play 2.1 框架。这是我的代码片段:

    <h2>First Serial</h2>
    <textarea id="firstSerial" style="width:600px;height:50px"></textarea><br/>

    <h2>Second Serial</h2>
    <textarea id="secondSerial" style="width:600px;height:50px"></textarea><br/><br/>

    <script>
        function grabTheSerials() {
            var firstSerial = document.getElementById('firstSerial').value;
            var secondSerial = document.getElementById('secondSerial').value;

            location.href="@routes.App.function(" + firstSerial + ", " + secondSerial + ")";
        }
    </script>

    <input type="button" value="execute-button" onclick="grabTheSerials()"/>

代码实际上并没有传递变量。我做了控制台调试,它实际上只是通过了+firstSerial+。我能做些什么来解决这个问题?

4

1 回答 1

1

如果我正确理解您的问题,这是我建议您执行的操作。

对于 HTML 表单:

<form method='post' action='@routes.App.function()'>
<h2>First Serial</h2>
    <textarea name="firstSerial" id="firstSerial"\ rows="4" cols="40"></textarea>

    <h2>Second Serial</h2>
    <textarea name="secondSerial" id="secondSerial" rows="4" cols="40"></textarea>

    <input type="submit" value="execute-button" />
</form>

然后,在 Play 控制器中,执行以下操作:

  public static Result function(){
        RequestBody body = request().body();
        Map<String, String[]> f = body.asFormUrlEncoded();
        String serial1 = f.get("firstSerial")[0];
        String serial2 = f.get("secondSerial")[0];
     //Do something with serial1 and serial2, like redirect to a new controller,
     //render a custom template, save in a database, or process
     //any other way you need.
}

或这个:

public static Result function(){
     DynamicForm dynamicForm = DynamicForm.form().bindFromRequest();
     String serial1 = dynamicForm.get("firstSerial");
     String serial2 = dynamicForm.get("secondSerial");
     //Do something with serial1 and serial2, like redirect to a new controller,
     //render a custom template, save in a database, or process
     //any other way you need.
    }

确保您的routes文件也为此设置:

POST /function controllers.App.function()

于 2013-07-02T15:48:39.087 回答