Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
Cómo cambiar el texto <b-button> después de hacer clic con una identificación única

Estoy trabajando con BootstrapVue . Tengo un b-button en mi plantilla donde quiero cambiar su texto después de hacer clic en él.

Estoy pasando mi item.id único generado a mi secuencia de comandos, pero esto no funciona en el caso de que se cambie cada b-button text y no solo en el que estoy haciendo clic. ¿Cuál es el problema aquí?

Debería poder copiar, pegar el código y debería funcionar.

Tenga en cuenta que es solo una réplica de mi código abreviado en el código necesario, por lo que el código no debe cambiarse tanto.

Mi plantilla:

 <div v-for="item in inputs" :key="item.id"> <b-button @click="changeText(item)">{{btnText}}</b-button> </div> <b-button @click="addInput">Add Input</b-button>

Mi guion:

 data() { return { collapsed: [true], id: null, inputs: [{id: 0}], btnText: "It's false", } }, methods: { changeText(item) { this.collapsed[item.id] = !this.collapsed[item.id] if(this.collapsed[item.id] === true) { this.btnText = "It's true" } else if(this.collapsed[item.id] === false) { this.btnText = "It's false" } }, addInput() { this.inputs.push({ id: this.id += 1, }) this.collapsed.push(true); } }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

  1. En lugar de tener matrices separadas para almacenar los diferentes campos, muévalos a la matriz de objetos de inputs[] , de modo que cada item en v-for tenga su propio id , collapsed y btnText .

  2. Luego actualice changeText() para hacer referencia a los campos dentro del argumento del item .

  3. También actualice la plantilla para usar el campo item.btnText .

 <script> export default { data() { return { inputs: [{ id: 0, collapsed: true, btnText: `It's true` }], 1️⃣ } }, methods: { changeText(item) { item.collapsed = !item.collapsed 2️⃣ if (item.collapsed) { item.btnText = `It's true` } else { item.btnText = `It's false` } }, addInput() { this.inputs.push({ 1️⃣ id: this.id++, collapsed: true, btnText: `It's true`, }) }, }, } </script> <template> <div> <div v-for="item in inputs" :key="item.id"> 3️⃣ <b-button @click="changeText(item)">{{ item.btnText }}</b-button> </div> <b-button @click="addInput">Add Input</b-button> </div> </template>

manifestación

about 4 years ago · Juan Pablo Isaza Report

0

Puede mantener un objeto para btnText como

 data() { return { collapsed: [false], id: null, inputs: [{id: 0}], btnText: {0: "It's false"} //Change added } }, methods: { changeText(item) { this.collapsed[item.id] = !this.collapsed[item.id] if(this.collapsed[item.id] === true) { this.btnText[item.id] = "It's true"; //Change added } else if(this.collapsed[item.id] === false) { this.btnText[item.id] = "It's false"; //Change added } }, addInput() { this.inputs.push({ id: this.inputs.length, // Change added }) this.btnText[this.inputs.length] = 'It's false'; //Change added this.collapsed.push(true); } }

y su plantilla debe ser como

 <div v-for="item in inputs" :key="item.id"> <b-button @click="changeText(item)">{{btnText[item.id]}}</b-button> <!-- Change added --> </div> <b-button @click="addInput">Add Input</b-button>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!