简体   繁体   中英

Vue js model binding for jQuery date picker

I am using jQuery date picker and its working perfectly. I am in Vue component. when I click the date input field it shows date picker but when I choose one date and see the result in the console I got nothing. Date is not bind.

<form @submit.prevent="search()">
    <div class="card">
        <div class="card-body">
            <div class="card-header"></div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-8">
                        <input type="text" v-model="date"  class="form-control" id="datepicker" >
                    </div>
                    <div class="col-md-4">
                        <button class="btn btn-primary" >Search</button>

                    </div>
                    
                </div>
                
            </div>
        </div>
        
    </div>
    </form>

<script>
    export default {
        data(){
            return{
                date:''
            }
        },
        mounted() {
            console.log('Component mounted.')
        },
        methods:{
            search(){
         //I want to get user chooses date here so that I can send to endpoint
                console.log(this.date)//got nothing here
          
            },
          

        },
   
    }
</script

when i click the search button, i should get the date in console but i got nothing, How to get current selected date by user? Thank you

You can bind the input inside mounted hook with the jquery date picker.

 new Vue({ el: '#app', data() { return { date: '' } }, mounted() { console.log('Component mounted.'); let selfInstance = this; $('#datepicker').datepicker({ onSelect: function(selected, datePicker) { selfInstance.date = selected; } }); }, methods: { search() { //I want to get user chooses date here so that I can send to endpoint console.log(this.date) //got nothing here }, }, })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet"> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <form id="app" @submit.prevent="search"> <div class="card"> <div class="card-body"> <div class="card-header"></div> <div class="card-body"> <div class="row"> <div class="col-md-8"> <input type="text" v-model="date" class="form-control" id="datepicker"> </div> <div class="col-md-4"> <button class="btn btn-primary">Search</button> </div> </div> </div> </div> </div> </form>

Note : this.date will not work inside the jquery blocks. So an instance should be defined to assign with vue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM