Enable the field when the checkbox is selected using VueJS

Hello there, I'm encountering an issue with my app. I am attempting to unlock a field if the checkbox next to it is selected and then save this value to an array. However, when I click on the checkbox, the field does not unlock. Below is the code snippet:

  <ul>
    <li v-for="oneFragrance in fragrance" v-bind:key="oneFragrance.id">
      <input type="checkbox" :value="oneFragrance.nameOfFragrance" :id="oneFragrance.id" v-model="checkedComponents"> {{oneFragrance.nameOfFragrance}}
      <input type="text"  :disabled="checkedComponents" v-model="valuesOfComponents">
    </li>
  </ul>

Answer №1

<ul>
<li v-for="(onePerfume , index) in perfumeList" v-bind:key="onePerfume.id">
  <input type="checkbox" :value="onePerfume.perfumeName" :id="onePerfume.id" v-model="selectedPerfumes[index]"> {{onePerfume.perfumeName}}
  <input type="text"  :disabled="selectedPerfumes[index]" v-model="componentsValues">
</li>

included in your dataset

var application = new Vue({
  el: '#application',
  data: {
    selectedPerfumes: []
  }
})

Answer №2

One possible solution could be to update the v-model of the initial input with a :click event

 <ul>
<li v-for="oneFragrance in fragrance" v-bind:key="oneFragrance.id">
  <input type="checkbox" :value="oneFragrance.nameOfFragrance" :id="oneFragrance.id" :click="checkedComponents = !checkedComponents"> {{oneFragrance.nameOfFragrance}}
  <input type="text"  :disabled="checkedComponents" v-model="valuesOfComponents">
</li>

This adjustment should ensure that the value consistently updates and the text input becomes enabled

Answer №3

To ensure the checkbox saves a value in an array, consider implementing the following code snippet:

<ul>
    <li v-for="oneFragrance in fragrance" v-bind:key="oneFragrance.id">
    <input type="checkbox" :value="oneFragrance.nameOfFragrance" :id="oneFragrance.id" v-model="checkedComponents"> {{oneFragrance.nameOfFragrance}}
    <input type="text" :disabled="!checkedComponents.includes(oneFragrance.nameOfFragrance)" v-model="valuesOfComponents">
    </li>
</ul>

Remember to define checkedComponents: [] within your data() function

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

The perplexing results received when using the npm outdated command

Could someone provide an explanation for the meaning behind this output? $ npm --version 3.10.8 $ npm -g outdated npm Package Current Wanted Latest Location npm 3.10.8 4.0.2 3.10.9 As stated in the documentation, the "Wanted" column should d ...

Using a function as a variable within jQuery's .click() method

Currently, I am utilizing a tabbed interface that is being managed by a jQuery function to handle the click events of my tabs. $(document).ready(function () { $('a#foo').click(function() { //content, various calls return fal ...

Ways to center vertically aligned buttons within cards in a React application with Material-UI

I've encountered an issue with my ReactJS project using material-ui. I created 3 cards, each with a paragraph of varying lengths. This caused the buttons to be misaligned vertically in each card, as the position differs due to paragraph size differenc ...

The function RegisterClientScriptInclude seems to be malfunctioning inexplicably

I've been struggling for over 2 days trying various solutions and searching online, but I can't seem to get RegisterClientScriptInclude to function properly like everyone else. For starters, I am using .NET 3.5 Ajax and incorporating javascript ...

Tips for showcasing a drop-down menu using Jquery

I am currently utilizing jQuery to showcase a drop-down menu. It is successfully working for a single menu and displaying the appropriate drop-down. However, when I attempt to use more than one menu, it displays all of the drop-down menus simultaneously. I ...

Restrict the quantity of user responses with JavaScript

For this questionnaire, users are limited to selecting and ranking only 3 out of the 5 listed Social Apps. I am currently using questback to build the survey, but I require assistance in implementing a JavaScript condition. <table> <tr> & ...

Ids that are dynamically generated in a unique manner

My latest project involves creating a website that allows users to drag elements onto the screen using JavaScript and jQuery. I want to save these elements in a database so they can be redrawn when users revisit the site. To make this work, each element n ...

Modifying an element within a nested array

I am struggling to create a JavaScript function that can update the "checked" value within an object in an array structured as follows: array:[ { id:1, name:"foo", options: [ {id:"one", checked: false}, {id:"two", checked: true} ] ...

Struggling to integrate vue js into Laravel

I recently developed a character counter feature for SEO input fields using Vue JS. It functioned flawlessly on jsfiddle, however, upon integrating it with Laravel, I encountered an issue. Instead of displaying the number of characters remaining, it showed ...

Error Encountered: POST method not supported in ajax request using djangoIs this a

I am currently encountering an issue while trying to pass form data values through ajax. I keep getting a method not allowed error when attempting to add a comment on a blog post. The form below is located inside the blog_detail page: <form id="co ...

At the pinnacle of the page while operating inside an iFrame - crossing domains

I've been searching for a solution here on SO, but I can't seem to figure out what's wrong. Here's the situation: I have a page hosted on DOMAIN1.COM. It's a form with a submit button and a lot of jQuery functionality. The form is ...

Issue with dynamically passing select values to pop-up link in Firefox

My website has a select dropdown feature that triggers a confirmation pop up with the same selection when the user makes a choice. Upon clicking Submit, a new window opens with the corresponding link. Everything works smoothly in Safari, Chrome, and Opera ...

When a form contains a ViewChild element, changes to the ViewChild element do not automatically mark the

Let's set the stage: MainComponent.html <form #someForm > <input type="text" name="title" [(ngModel)]="mainVar" /> <child-component /> <input type="submit" [disabled]="someForm.form.pristine" /> </form> ChildComp ...

When examining an element in an Angular application using Chrome Dev Tools, why do I not observe raw HTML code?

Just as the title suggests, my question is: Even if browsers don't understand Angular, why am I able to see Angular elements while inspecting the DOM despite using AOT (Ahead-Of-Time Compilation) which means there is no Angular compiler in the browse ...

Using v-model with checkboxes in vue.js: A Step-by-Step Guide

How can I use a checkbox with a v-model in Vue2.js? <input type="checkbox" value="test" :checked="selected"/> I need the value of the checkbox to be test, and I also require two-way binding with the prop named selected, ...

jQuery does not change the scroll position of elements

I'm looking to implement a feature on my website where certain points act as "magnets" and when the user is near one of these points, the window automatically scrolls to the top. I found a jQuery plugin that does something similar which you can check ...

Is it more efficient to pass session variables through an ajax function, or should I access them directly within the script?

I am currently working on a page that utilizes an ajax function to retrieve updates from another page. The function requires the user's id, which is obtained from a session variable, to fetch any new updates. These updates are then displayed in a spec ...

JavaScript analyzing a file's content one line at a time

Here is the image of a file (screenshot) Could you provide guidance on how to parse this file line by line using JavaScript and display each line in its own HTML element? function loadTxt() { jQuery.get('http://localhost:8000/np/nowPlaying.t ...

Tips on showcasing a JSON object containing two arrays in HTML using a pair of for loops

I am facing an issue with displaying data from two tables in my PHP script. The personal information is being displayed correctly, but the books related to each person are not showing up. I suspect that my approach might not be efficient enough for handlin ...

The data in my AngularJS model will only refresh when the button is clicked twice

I am encountering an issue with a select list box and a button in my code. The aim is to filter the model displayed in the select list box based on the selectId received from clicking the button. The problem arises when the model updates only after clicki ...