У меня есть сообщение «Привет!Я хотел бы заменить текст «Там» значением, вводимым в поле ввода.
Я пытался использовать директиву
HTML
<div id="updateName">
<!--form sec-->
<section class="animated container-fluid align-center sec-ptop">
<h3 class="salutation">Hey <span>{{inputName}}There</span>, happy to hear from you.</h3>
<div>
<form name="contactform" method="post" class="row form-horizontal" role="form">
<div class="form-group input--josh col-sm-6">
<div class="input-wrap">
<input autocomplete="off" v-init type="text" v-model="inputName" class="form-control input__field input input__field--josh" id="inputName" name="inputName" placeholder="Name" value="" required />
<label class="input__label input__label input__label--josh input__label--josh-color-1 input__label--josh input__label--josh-color-1"></label>
</div>
</div>
</form>
</div>
</section>
</div>
vue
var app = new Vue({
el: '#updateName',
data: {
inputName: 'There'
},
directives: {
init: {
bind(el){
el.value = el.getAttribute('value');
el.dispatchEvent(new Event('input'));
}
}
},
});
HTML
<div id="updateName">
<!--form sec-->
<section class="animated container-fluid align-center sec-ptop">
<h3 class="salutation">Hey <span>{{inputName}}There</span>, happy to hear from you.</h3>
<div>
<form name="contactform" method="post" class="row form-horizontal" role="form">
<div class="form-group input--josh col-sm-6">
<div class="input-wrap">
<input autocomplete="off" type="text" v-model="inputName" v-on:keyup.enter="replaceText(inputValue)" class="form-control input__field input input__field--josh" id="inputName" name="inputName" placeholder="Name" required />
<label class="input__label input__label input__label--josh input__label--josh-color-1 input__label--josh input__label--josh-color-1"></label>
</div>
</div>
</form>
</div>
</section>
</div>
vue
var app = new Vue({
el: '#updateName',
data: {
inputName: 'There'
},
methods: {
replaceText: function(inValue) {
this.inputName = this.inputName + inValue
}
}
});
На странице загрузки
Привет, рад вас слышать.
Имя
Во время выполнения
Привет, Джон, рад слышатьот вас.
Джон
Время работы