Vue Form Inputs
On the Vue Forms and v-model pages previously in this course, we have seen a few samples of form inputs.
More Vue form input examples, including radio buttons, checkboxes, drop-down menus, and standard text input fields, are gathered on this page.
Radio Buttons
Only one radio button can be selected at a time because buttons that are part of the same choice must have the same name.
The radio button input value is captured using v-model, much like with all other inputs in Vue, but the <input type=”radio”> tag also needs to have its value property explicitly set.
In a Vue form, radio buttons can be used as follows:
Example
App.vue:
Radio Buttons in Vue
Submitted choice:
{{ inpValSubmitted }}
Checkboxes
The values for the ticked checkboxes are collected in the array when checkbox inputs (<input type=”checkbox”>) are linked to the same array with v-model:
Example
App.vue:
Checkbox Inputs in Vue
Submitted answer:
{{ inpValSubmitted }}
Drop-down List
A drop-down list is made up of <option> tags enclosed in a <select> tag.
Given values to the <option> tags, we must connect the <select> tag with v-model when utilizing a drop-down list in Vue:
Example
App.vue:
Drop-down List in Vue
Submitted answer:
{{ inpValSubmitted }}
< select multiple >
The <select> tag’s multiple attribute expands the drop-down list so that we can select more than one choice.
The ‘ctrl’ and ‘cmd’ keys are required for Windows users to select several options, and respectively for Mac users.
We must link the <select> tag with the v-model and assign values to the <option> tags in order to use <select multiple> in Vue:
Example
App.vue:
Select Multiple in Vue
Depending on your operating system, use the 'ctrl' or the 'command' key to select multiple options.
Submitted answer:
{{ inpValSubmitted }}
Read Only Form Inputs
When form inputs are used with v-model, a two-way binding is established; that is, when the Vue data instance changes, the input value attribute changes as well.
We are unable to use v-model for read-only form inputs, such as <input type=”file”>, because the value attribute cannot be modified from the Vue data instance.
To update the Vue data instance for read-only form inputs like as <input type=”file”>, we must use @change to invoke a method:
Example
App.vue:
Input Type File
Submitted answer:
{{ inpValSubmitted }}
Information: The file path C:\fakepath\ appears before the provided file name in the example above. This keeps dangerous malware from figuring out the file system of the user.
Other Form Inputs
In contrast to the form inputs listed before, where we were required to supply a value for the value attribute, the form inputs listed below allow the user to supply the value:
- <input type=”color”>
- <input type=”date”>
- <input type=”datetime-local”>
- <input type=”number”>
- <input type=”password”>
- <input type=”range”>
- <input type=”search”>
- <input type=”tel”>
- <input type=”text”>
- <input type=”time”>
- <textarea>
For this type of form input, the user provides the value already; thus, in Vue, all that’s required is connecting the input to a data property using v-model.
Using <input type=”range”> in Vue is as follows:
Example
App.vue:
And here’s how you utilize Vue with <input type=”color”>:
Example
App.vue:
Moreover, this is how Vue uses <textarea>:
Example
App.vue: