Clear the disabled input value in VueJS

My goal is to have the input disabled and empty. If the LEVEL2 input is empty, then both LEVEL3 and LEVEL4 should also be disabled and empty.

If the LEVEL3 input is empty, then LEVEL4 should be disabled and empty.

The disabled code is functioning properly, but the value still remains. The current code looks like this:

<el-form-item :prop="'materialInfo.' + e_key + '.LEVEL2'" :rules="level2Rules" class="u_f_left">
  <span>-</span>
  <el-input v-model="e_val.LEVEL2" :value="e_val.LEVEL2" placeholder="Level 2" class="wid70" @blur="joinLevel(e_key)" clearable></el-input>
</el-form-item>
<el-form-item :prop="'materialInfo.' + e_key + '.LEVEL3'" :rules="level3Rules" class="u_f_left">
  <span>-</span>
  <el-input :disabled="e_val.LEVEL2?false:true" v-model="e_val.LEVEL3" :value="e_val.LEVEL3" placeholder="Level 3" class="wid70" @blur="joinLevel(e_key)" clearable></el-input>
</el-form-item>
<el-form-item :prop="'materialInfo.' + e_key + '.LEVEL4'" :rules="level4Rules" class="u_f_left">
  <span>-</span>
  <el-input :disabled="e_val.LEVEL3?false:true" v-model="e_val.LEVEL4" :value="e_val.LEVEL4" placeholder="Level 4" class="wid70" @blur="joinLevel(e_key)" clearable></el-input>
</el-form-item>

A screenshot of how it currently behaves can be seen here.

I've tried adjusting the conditions in the v-model, like so:

<el-input :disabled="e_val.LEVEL2?false:true" v-model="e_val.LEVEL2?e_val.LEVEL3:EMPTYVALUE" :value="e_val.LEVEL3" placeholder="Level 3" class="wid70" @blur="joinLevel(e_key)" clearable></el-input>

The variable EMPTYVALUE is defined as EMPTYVALUE: '',.

However, I encountered some errors when trying this method and it did not compile successfully. Additionally, I would prefer to avoid using the blur or change events if possible.

Answer №1

const app = new Vue({
  el: "#app",
  data() {
    return {
      boxes: new Array(5),
    }
  },
  computed: {
    lastFilledBox() {
      let lastFilledBoxIndex = 0;
      for (let i = 0; i < this.boxes.length; i++) {
        if (this.boxes[i] && this.boxes[i].length && i == lastFilledBoxIndex) {
          lastFilledBoxIndex++;
        } else {
          this.boxes[i] = '';
        }
      }
      return lastFilledBoxIndex;
    }
  }
});
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

input[type=text] {
  width: 50px;
  height: 30px;
  font-size: 16px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <input type="text" v-for="(item, index) in boxes" :key="index"
         v-model="boxes[index]" :disabled="index > lastFilledBox" />
</div>

Answer №2

If you want to achieve a similar functionality, you can use the following approach: include a watch feature on each input element.

new Vue({
  el: "#app",
  data: {
      input: {
        one: "",
        two: "",
        three: "",
        four: ""
      }
  },
  watch: {
    input: {
      handler(val) {
        !val.one ? (this.input.two = "") : "";
        !val.two ? (this.input.three = "") : "";
        !val.three ? (this.input.four = "") : "";
      },
      deep: true
    }
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
    <input type="text" v-model="input.one" placeholder="one" />
    <input type="text" v-model="input.two" placeholder="two" :disabled="!input.one" />
    <input type="text" v-model="input.three" placeholder="three" :disabled="!input.two" />
    <input type="text" v-model="input.four" placeholder="four" :disabled="!input.three" />
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Refresh component when mobx store is modified

I am utilizing chart.js to display real-time price changes from the backend. The backend updates the frontend with new prices as they change, and stores the priceData (array) in a mobx store named priceChartStore. I need to continuously update the chart as ...

Is it necessary to specify a data type for the response when making an AJAX POST request?

After carefully reviewing my code, I have ensured that all the variables in my JavaScript match and are properly set up. Additionally, I have confirmed that all the variables in my data array for my AJAX request correspond correctly to my $_POST['var& ...

Navigating between functions in JavaScriptBy switching from one function to another

I'm facing an issue with two functions where I am trying to return a value from the second function. Unfortunately, I keep getting an alert with undefined. Any ideas on what might be the problem with my code? function funcA(){ var test = funcB("h ...

Implementing dynamic checkbox values depending on a selection from a dropdown menu in Angular

In my checkbox list, I have various Samsung mobile models and two offers available. $scope.offers = [ { id: "as23456", Store: "samsung", Offer_message:"1500rs off", modalname: "Samsung Galaxy You ...

Developing mobile-friendly applications with AngularJS, incorporating ngTouch into directives

Utilizing ngTouch to eliminate the delay on mobile devices has been effective, however, I have encountered an issue where clicking on an image does not trigger the expected behavior. In my application, clicking on an image is supposed to activate a directi ...

Deactivating choices in Autoselect from Material UI

I am attempting to implement a feature in the autocomplete of material ui where options are disabled based on a specific condition. Each table row contains an autocomplete field, and when an option is selected in one row, it should be disabled in the next ...

What is the best way to retrieve user input from a form within an Ajax.ActionLink?

My webpage is designed to be simple, featuring a picture and a comment section that functions as a partial view. I am looking to only refresh the partial view when a new comment is submitted. Here's what I have coded: <tr> & ...

JavaScript conditional using only conditions with ampersand operators that will evaluate only the first half of

I'm facing an issue with the conditional statement in my code. Currently, if selectedItem = null, it will set setOption = dropdownItems[0].value but won't set setPage = 1. The same problem occurs with the line below it. Can anyone provide feedbac ...

Error arises when using ODBC PDO alongside Ajax functionality

I've encountered a problem while working on a project at my job. I'm using PDO queries and ajax to generate dynamic elements, but I can't seem to fix a specific issue. The problem is that regardless of the option selected in two dynamically ...

Retrieve content from the pushState state object

$(function(){ $('button').click(function(e){ e.preventDefault(); var nextUrl = 'page1.html'; var previousUrl = window.location.href; $.get(nextUrl, function(data){ $('body').ht ...

In React Router version 4, it is stated that each router is only allowed to have a single child element when using multiple routes

I'm currently working on implementing a sidebar alongside the main content area using react-router-dom. Instead of just rendering <Sidebar/> directly, I want to pass it the location prop due to another issue where clicking a <Link/> in the ...

Issue with updating AngularJS model within nested ng-repeat block

My issue involves using nested ng-repeat to iterate through properties of an array of objects and connecting the model of the objects' values to inputs. The problem arises when the values inside the inputs are changed, the model fails to update. va ...

javascript change string into an array of objects

let dataString = "{lat: -31.563910, lng: 147.154312};{lat: -33.718234, lng: 150.363181};{lat: -33.727111, lng: 150.371124}"; let dataArray = dataString.split(';'); Produces the following output: let dataArray = [ "{lat: -31.563910, lng: 147 ...

Can a client component in NextJs retrieve data from a server component?

Apologies for the way the question is phrased. I have a server component named auth.ts that retrieves user details after they log in. This server side component is located at //auth.ts export const validateRequest = cache( async (): Promise< { use ...

What is the best way to combine two arrays using Mongoose?

Group 1 = [X, , , , ,X] Group 2 = [ , , ,O, , ] I am looking for a way to combine group 1 with group 2 in order to achieve the following arrangement: [X, , , O, ,X] without simply replacing Group 1 with Group 2.. Here is the code snippet I have so far: ...

Vue.js compatibility issue with SelectBoxIt jq plugin causing malfunction

On a page with numerous dynamically generated select boxes, I am looking to incorporate the jQuery selectBoxIt plugin from . Using Vue js, where is the best placement for the following code snippet to connect the plugin with the select elements? $('. ...

Storing objects in localStorage using VueJS

Is there a way to send all theme configuration settings as objects instead of recording them one by one? Can you provide an example if possible? Also, how do I set it on the store? I'm new to storage management so kindly provide guidance accordingly. ...

Localhost server experiencing issues with uploading files through Express framework

I'm currently trying to integrate the ng file upload feature into my node application running locally on localhost. Following the example shown here, I encountered an issue when attempting to change the download directory. file.upload = Upload.upload ...

Accessing a Kendo Grid instance within an Angular directive

Continuing from the previous question, which can be found here, I have an additional query that I felt warranted a separate post. The link provided above demonstrates how to obtain a grid instance in Angular, credit to Lars for that. Building upon the ex ...

Sending state data in an Axios POST request

I'm trying to include a state in an axios post request to send data to my backend, but I'm getting an error saying the state I selected is not defined. Here's the code snippet: React frontend import React, { Component } from "react&qu ...