Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

144
Vistas
Vue.js How to mirror input field with v-model and computed property and a checkbox

I am trying to figure out how to properly mirror two input fields and dynamically change them based on a checkbox value. This is the best what I can think of using computed property with get and set features. The problem is when the user types (1)"ABC" in shipping details, (2)unchecks (copy_chekcbox=false), (3)types "123" in billing details, and (4)checks back (copy_chekcbox=true) (5)the billing details doesn't revert to shipping details, which is what I need. Image with steps

Also, is there any shorter and more optimal way to mirror two fields and change their values based on checkbox value?

Thank you in advance.

<!DOCTYPE html>
<html>

<head>
  <title>My first Vue app</title>
  <script src="https://unpkg.com/vue"></script>
</head>

<body>
  <div id="app">

    <p>Shipping details</p>
    <input v-model="shipping" type="text" />

    <p>Same as shipping: <input type="checkbox" v-model="copy_chekcbox" /></p>

    <p>Billing details</p>
    <input v-model="billing" type="text" :disabled="copy_chekcbox" />

  </div>

  <script>
    var app = new Vue({
      el: '#app',
      data: {
        shipping_data: '',
        billing_data: '',
        copy_chekcbox: true,
      },
      computed: {
        shipping: {
          get: function() {
            return this.shipping_data
          },
          set: function(newValue) {
            if (!this.copy_chekcbox) {
              this.shipping_data = newValue
            } else {
              this.billing_data = newValue
              this.shipping_data = newValue
            }
          },
        },
        billing: {
          get: function() {
            return this.billing_data
          },
          set: function(newValue) {
            if (!this.copy_chekcbox) {
              this.billing_data = newValue
            } else {
              this.billing_data = this.shipping_data
            }
          },
        },
      }
    })
  </script>
</body>

</html>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

In my opinion I think the easiest way is use two inputs with v-if:

<p>Shipping details</p>
<input v-model="shipping" type="text">

<p>Same as shipping: <input type="checkbox" v-model="copy_chekcbox"></p>

<p>Billing details</p>

<template v-if="copy_chekcbox">
  <input v-model="shipping" type="text">
<template>
<template v-else>
  <input v-model="billing" type="text">
</template>

The main advantage of this method is that your JavaScript code remains simple.

about 4 years ago · Juan Pablo Isaza Denunciar

0

try this:

billing: {
          get: function() {
            return this.copy_checkbox ? this.shipping_data : this.billing_data
          },

it should rerun the computed function for your billing data when you change copy_checkbox and thus update your billing_data.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda