1

我从表单中获取值并使用这些值在我的游戏项目中创建 Journey 对象,但我需要扩展它以便我可以在后台添加其他值。下面是我当前的表单,它从用户输入中获取所有值。

@form(routes.Application.createJourney()) {

    <fieldset>

    @inputText(journeyForm("start_loc"), '_label -> "Starting Location", 'placeholder -> "E.g. 1 Main Street")
    @inputText(journeyForm("end_loc"), '_label -> "End Location", 'placeholder -> "E.g. 1 High Street")
    @select(
        journeyForm("participant_type"), 
        options = options("Driver"->"Driver", "Passenger"->"Passenger"),
        '_label -> "Travel as",
        '_error -> journeyForm("participant_type").error.map(_.withMessage("Select participant type"))
        )
    @inputDate(journeyForm("date"), '_label -> "Date")
    @helper.input(journeyForm("Time")) { (id,name,value,args) => <input type="time" name="@name" id="@id" @toHtmlArgs(args)> } 

    </fieldset>
    <div class="actions">
        <input type="submit" value="Create" class="btn primary"> 
    </div>

我不太确定如何将 javascript 与这些 scala 模板生成器一起使用,并且我有一个我想使用的 javascript 函数。我表单中的两个输入用于位置,我希望能够使用 javascript 检索这些位置,以使用 Google Maps API 获取它们的 lat/lng 值。以下是一个位置的 javascript:

  <script type="text/javascript">
  function codeAddress() {
    var geocoder;
    geocoder = new google.maps.Geocoder();
    var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        alert(results[0].geometry.location);
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }
  </script>

所以有两件事我真的需要做。从表单中检索 inputText 以在 javascript 函数中使用。在我的 javascript 函数中使用变量在后台填充表单参数之一。通常我只是一个 document.getElementByID 但我认为这不适用于表单助手?

4

1 回答 1

0

您可以使用表单助手设置元素的 id。一旦它在视图中呈现,用 javascript 检索它应该不是问题。

于 2013-03-12T13:28:47.640 回答