Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make input read only based on particular value in Vue?

Tags:

How to make an input field read only based on Vue data?

For example:

<select class="form-control"          id="selectCategory"          :disabled="cat_id >=              1"          name="cat_id"> 

I want to make the field read only but not disabled. How can I achieve this?

like image 493
Neha Avatar asked Feb 13 '18 10:02

Neha


People also ask

How do I make an input element read-only?

The readonly attribute is a boolean attribute. When present, it specifies that an input field is read-only. A read-only input field cannot be modified (however, a user can tab to it, highlight it, and copy the text from it).

How do I get input value from Vue?

The v-model directive in Vue creates a two-way binding on the input element. All you need to do is simply declare a v-model directive for the input element and reference it to get the value. Every time the input value changes, “myInput” model will keep track of the changes.

What is the use of Read-Only attribute?

Read-only is a file attribute which only allows a user to view a file, restricting any writing to the file. Setting a file to “read-only” will still allow that file to be opened and read; however, changes such as deletions, overwrites, edits or name changes cannot be made.

How do you clear input after submitting Vue?

In vue. js, we use the v-model directive to create a two-way data binding between the input field and vue data property, so that we can clear an input field value by setting the empty string (" ") to the data property.


1 Answers

Please note that, according to HTML specs, the select tag in HTML doesn't have a readonly attribute.

However, in general case, I'd go with something like this:

<input class="form-control" id="selectCategory" :readonly="cat_id >= 1"> 

Basically, the documentation says that if an attribute value evaluates to false, then the attribute being omitted. See here for further details.

like image 76
P3trur0 Avatar answered Sep 18 '22 08:09

P3trur0