フォームから値を取得し、それらの値を使用して Play プロジェクトで 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>
これらの scala テンプレート ジェネレーターで JavaScript を使用する方法がよくわかりません。使用したい JavaScript 関数があります。私のフォームの 2 つの入力は場所用です。JavaScript を使用してこれらの場所を取得し、Google Maps API を使用して緯度/経度の値を取得できるようにしたいと考えています。以下は、1 つの場所の 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>
ですから、私が本当にしなければならないことが 2 つあります。JavaScript 関数で使用するために、フォームから inputText を取得します。JavaScript 関数で変数を使用して、フォーム引数の 1 つをバックグラウンドで入力します。通常、私は document.getElementByID だけを使用しますが、これがフォーム ヘルパーで機能するとは思いませんか?