[英]vue, only bind or use v-model if button in modal is clicked
I'm having a problem with pre-populating data in a vue-modal on page load, so the modal is rendered and hidden meaning that every row creating a modal already renders a textarea and input that I want to pass to a function on button click.我在页面加载时在 vue-modal 中预填充数据时遇到问题,因此模式被渲染并隐藏,这意味着创建模式的每一行都已经渲染了我想要传递给按钮上的 function 的文本区域和输入点击。
How can I change this so that I only pass the textarea and hidden input to the props if the save button is clicked?如何更改此设置,以便仅在单击保存按钮时将 textarea 和隐藏输入传递给道具?
@foreach($results as $k => $results)
<tr class="" id="">
<td>
<a id="show-row-modal" @click="showModal = {{$k}}; getDetails('{{$results->contentID}}');">{{$results->number}}</a>
<modal v-if="showModal==={{$k}}" @close="showModal = false">
<h2 slot="header">{{$results->number}}-{{$results->name}}</h2>
<div slot="body">
<textarea style="width:100%; margin: 0 auto;" v-model="resultsContent">{{utf8_encode($results->content)}}</textarea>
<input type="hidden" value='{$results->contentID}' v-model="existingContentID" />
<button>save</button>
</div>
</td>
</tr>
@endforeach
<script type="text/x-template" id="row-modal-template">
<transition name="rowModal">
<div class="modal-mask">
<div class="modal-wrapper">
<div class="modal-container">
<div class="uk-grid">
<div class="modal-header uk-form-row uk-width-1-1">
<slot name="header">
default header
</slot>
</div>
<div class="modal-body uk-form-row uk-width-1-1">
<slot name="body">
default body
</slot>
</div>
</div>
</div>
</div>
</div>
</transition>
</script>
Vue.component('modal',{
template: '#row-modal-template'
})
new Vue({
el:'#app',
data: {
showModal: false,
existingCopyID: [''],
resultsContent: [''],
},
Using v-model means any change to those inputs will immediately update the piece of state they are bound to.使用 v-model 意味着对这些输入的任何更改都将立即更新它们绑定到的 state。 Instead, sounds like you want to use
refs
to only retrieve the input values on save click:相反,听起来您想使用
refs
仅在保存单击时检索输入值:
<textarea style="width:100%; margin: 0 auto;" ref="resultsContent">{{utf8_encode($results->content)}}</textarea>
<input type="hidden" value='{$results->contentID}' ref="existingContentID" />
... ...
handleSaveClick() {
// Grab the value from each input via this.$refs
console.log(this.$refs.resultsContent.value);
console.log(this.$refs.existingContentID.value);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.