diff --git a/src/guide/forms.md b/src/guide/forms.md index a4531a61..30b1d7da 100644 --- a/src/guide/forms.md +++ b/src/guide/forms.md @@ -1,25 +1,25 @@ -# Form Input Bindings +# フォーム入力バインディング -## Basic Usage +## 基本的な使い方 -You can use the `v-model` directive to create two-way data bindings on form input, textarea, and select elements. It automatically picks the correct way to update the element based on the input type. Although a bit magical, `v-model` is essentially syntax sugar for updating data on user input events, plus special care for some edge cases. +form の input 要素や textarea 要素、 select 要素に双方向データバインディングを付与するためには、`v-model`を使用することができます。`v-model`は、要素を更新する適切な方法を入力の種類に基づき自動的に選択します。少し魔法のようですが、本来`v-model`は糖衣構文(syntax sugar)であり、ユーザの入力イベントに応じてデータを更新し、さらにエッジケースに対する特別な配慮をしてくれます。 -::: tip Note -`v-model` will ignore the initial `value`, `checked` or `selected` attributes found on any form elements. It will always treat the current active instance data as the source of truth. You should declare the initial value on the JavaScript side, inside the `data` option of your component. +::: tip +`v-model` はフォーム要素の`value`属性や`checked`属性、`selected`属性の初期値を無視します。`v-model`は常に、現在アクティブなインスタンスの`data`を信頼できる情報源として扱います。初期値の宣言はJavaScript側、コンポーネントの`data`オプション内で行ってください。 ::: -`v-model` internally uses different properties and emits different events for different input elements: +内部的には、`v-model`は異なる input 要素に対し異なるプロパティを使用し、異なるイベントを送出します。 -- text and textarea elements use `value` property and `input` event; -- checkboxes and radiobuttons use `checked` property and `change` event; -- select fields use `value` as a prop and `change` as an event. +- text および textarea 要素には、`value`プロパティと`input`イベントを用います +- チェックボックスおよびラジオボタンには、`checked`プロパティと`change`イベントを用います +- select フィールドには、`value`プロパティと`change`イベントを用います ::: tip Note -For languages that require an [IME](https://en.wikipedia.org/wiki/Input_method) (Chinese, Japanese, Korean etc.), you'll notice that `v-model` doesn't get updated during IME composition. If you want to cater for these updates as well, use `input` event instead. +[IME](https://ja.wikipedia.org/wiki/%E3%82%A4%E3%83%B3%E3%83%97%E3%83%83%E3%83%88%E3%83%A1%E3%82%BD%E3%83%83%E3%83%89) を必要とする言語 (中国語、日本語、韓国語など) においては、IME による入力中に`v-model`が更新を行わないことに気づくでしょう。このような入力に対しても同様に扱いたい場合は、代わりに`input` イベントを使用してください。 ::: -### Text +### テキスト ```html @@ -33,7 +33,7 @@ For languages that require an [IME](https://en.wikipedia.org/wiki/Input_method)

-### Multiline text +### 複数行テキスト ```html Multiline message is: @@ -49,7 +49,7 @@ For languages that require an [IME](https://en.wikipedia.org/wiki/Input_method)

-Interpolation on textareas won't work. Use `v-model` instead. +textarea への挿入は機能しません。代わりに`v-model`を用いてください。 ```html @@ -59,9 +59,9 @@ Interpolation on textareas won't work. Use `v-model` instead. ``` -### Checkbox +### チェックボックス -Single checkbox, boolean value: +単一のチェックボックスと boolean 値: ```html @@ -75,7 +75,7 @@ Single checkbox, boolean value:

-Multiple checkboxes, bound to the same Array: +同じ配列にバインドされた複数のチェックボックス: ```html
@@ -107,7 +107,7 @@ Vue.createApp({

-### Radio +### ラジオ ```html
@@ -138,9 +138,9 @@ Vue.createApp({

-### Select +### セレクト -Single select: +単一のセレクト: ```html
@@ -172,10 +172,10 @@ Vue.createApp({ :::tip Note -If the initial value of your `v-model` expression does not match any of the options, the ``要素は *未選択* の状態で描画されます。これにより iOS では最初のアイテムが選択できなくなります。なぜなら iOS はこのような場合に `change` イベントを発火させないためです。したがって、上記の例で示したように、`value`を持たない`disabled` なオプションを追加しておくことをおすすめします。 ::: -Multiple select (bound to Array): +複数個のセレクト(配列にバインド): ```html - + - + ``` -But sometimes we may want to bind the value to a dynamic property on the current active instance. We can use `v-bind` to achieve that. In addition, using `v-bind` allows us to bind the input value to non-string values. +しかし、現在アクティブなインスタンスの動的なプロパティに値をバインドしたいときがあるかもしれません。それを実現するためには`v-bind`を使用できます。さらに、`v-bind`を使用することで入力値に文字列以外の値もバインドできるようになります。 -### Checkbox +### チェックボックス ```html ``` ```js -// when checked: +// チェックされているとき: vm.toggle === 'yes' -// when unchecked: +// チェックされていないとき: vm.toggle === 'no' ``` :::tip Tip -The `true-value` and `false-value` attributes don't affect the input's `value` attribute, because browsers don't include unchecked boxes in form submissions. To guarantee that one of two values is submitted in a form (e.g. "yes" or "no"), use radio inputs instead. +`true-value` と `false-value` 属性は input の `value` 属性には影響を及ぼしません。なぜならブラウザはチェックされていないチェックボックスをフォーム送信内容には含めないためです。二つの値(例: "yes" または "no")のうち一つが必ず送信されることを保証するには、代わりにラジオを使用してください。 ::: -### Radio +### ラジオ ```html ``` ```js -// when checked: +// チェックされているとき: vm.pick === vm.a ``` -### Select Options +### セレクトオプション ```html ``` ```js -// when selected: +// 選択されているとき: typeof vm.selected // => 'object' vm.selected.number // => 123 ``` -## Modifiers +## 修飾子 ### `.lazy` - -By default, `v-model` syncs the input with the data after each `input` event (with the exception of IME composition as [stated above](#vmodel-ime-tip)). You can add the `lazy` modifier to instead sync after `change` events: +デフォルトでは`v-model`は各`input`イベント後に入力値とデータを同期します([上述](#vmodel-ime-tip)のIME入力の例外はあります)。`lazy` 修飾子を加えることで、`change`イベント後に同期するよう変更できます。 ```html - + ``` ### `.number` -If you want user input to be automatically typecast as a number, you can add the `number` modifier to your `v-model` managed inputs: +ユーザ入力を自動的に number へ型キャストさせたい場合は、`v-model`で管理している input に`number`修飾子を加えることができます。 ```html ``` -This is often useful, because even with `type="number"`, the value of HTML input elements always returns a string. If the value cannot be parsed with `parseFloat()`, then the original value is returned. +これはしばしば有用です。なぜなら`type="number"`が書いてあったとしても HTML の input 要素は常に文字列を返すためです。値が`parseFloat()` でパースできない場合は、元々の値が返却されます。 ### `.trim` -If you want whitespace from user input to be trimmed automatically, you can add the `trim` modifier to your `v-model`-managed inputs: +ユーザ入力から空白を自動で取り除きたい場合は、`v-model`で管理している input に `trim` 修飾子を加えることができます。 ```html ``` -## `v-model` with Components +## コンポーネントの`v-model` + +> まだ Vue コンポーネントに慣れていない場合、この節は一旦スキップすることができます。 -> If you're not yet familiar with Vue's components, you can skip this for now. +HTML組み込みの input タイプが、常にあなたのニーズに適っているとは限りません。幸運にも、Vue コンポーネントによって、動作を隅々までカスタマイズ可能な再利用性のある入力フォームを自作することができます。それらのフォームに`v-model`を使うことも可能です!詳しくは、コンポーネントガイドの [カスタム input](./component-basics.html#using-v-model-on-components) を参照してください。 -HTML's built-in input types won't always meet your needs. Fortunately, Vue components allow you to build reusable inputs with completely customized behavior. These inputs even work with `v-model`! To learn more, read about [custom inputs](./component-basics.html#using-v-model-on-components) in the Components guide.