javascript - VueJS - 数据列表输入未清除值

标签 javascript html vue.js vue-component html-datalist

我有一个 vue 组件,可以扫描二维码并在成功后将一些信息输入数据库。这很好用。但是,一旦数据发送成功,我想清除我的数据列表输入字段,如下所示:$('#adduser').val('');,但没有任何效果。输入内容保持填写状态。

enter image description here

这是我目前的代码..

<template>
    <div>
        <p class="message">{{ error }}</p>
        <div class="qr-fullscreen">
            <p class="decode-result" style="color:#ccc">Scanned: <strong style="color:#fff">{{ name }}</strong>
                <br>
                <input name="adduser" id="adduser" @input="addParticipation(user.upn)" class="form-control user_input" type="text" v-model="user.upn" list="manual" placeholder="Manually add an attendee">
                <datalist id="manual">
                    <option v-for="user in users" v-bind:value="user.upn">{{user.firstname}} {{user.lastname}} (Year {{user.year}})</option>
                </datalist>
            </p>
            <qrcode-stream @decode="onDecode" @init="onInit" />
            <a v-bind:href="'/admin/sessions/'+this.session.id" class="qr-fullscreen-close">
                <i class="fa fa-times"></i>
            </a>
        </div>
    </div>
</template>

<script>
    import { QrcodeStream } from 'vue-qrcode-reader';

    export default {

        mounted() {
            $('.loading').hide();
        },

        components: { QrcodeStream },

        data () {
            return {
                result: '',
                error: '',
                name: 'No one scanned',
                user: [],
                objectItem: {},
            }
        },

        methods: {
            onDecode (result) {
                this.result = result;
                this.addParticipation(this.result);
            },

            addParticipation(upn){

                var last_character = upn[upn.length-1];
                if(isNaN(last_character)){}else{

                    let currentObj = this; // important?
                    axios.post('/api/session_capture', {
                        session_id: this.session.id,
                        upn: upn,
                        api_token: localStorage.getItem('api_token')
                    })
                        .then((response) => {
                            //var audio = new Audio(require('audio/snap.mp3'));
                            //audio.play();
                            if (response.data.message) {
                                this.message(response.data.message, 4000);
                            } else {
                                this.playSound();
                                this.message(response.data.name + ' added successfully', 2000);
                                this.name = response.data.name;
                                $('#adduser').val('');
                            }


                        })
                        .catch((error) => {
                            currentObj.output = error;
                            console.log(currentObj.output);
                        });
                }
            },
            message($message,$time){
                $('.message').text($message).fadeIn(500, function(){
                    setTimeout(function(){
                        $('.message').fadeOut(500);
                    }, $time);
                });
            },
            playSound() {
                const path = this.root+'/audio/snap.mp3';
                const audio = new Audio(path);
                var playPromise = audio.play();

                if (playPromise !== undefined) {
                    playPromise.then(_ => {
                        console.log('Did you hear that?');
                    })
                        .catch(error => {
                            console.log(`playSound error: ${error}`);
                        });
                }
            },

            async onInit (promise) {
                try {
                    await promise
                } catch (error) {
                    if (error.name === 'NotAllowedError') {
                        this.error = "ERROR: you need to grant camera access permisson"
                    } else if (error.name === 'NotFoundError') {
                        this.error = "ERROR: no camera on this device"
                    } else if (error.name === 'NotSupportedError') {
                        this.error = "ERROR: secure context required (HTTPS, localhost)"
                    } else if (error.name === 'NotReadableError') {
                        this.error = "ERROR: is the camera already in use?"
                    } else if (error.name === 'OverconstrainedError') {
                        this.error = "ERROR: installed cameras are not suitable"
                    } else if (error.name === 'StreamApiNotSupportedError') {
                        this.error = "ERROR: Stream API is not supported in this browser"
                    }
                    message(this.error);
                }
            }
        },

        props:[
            'session',
            'root',
            'users'
            ],
    }



</script>

<style>
    .message {
        position: fixed;
        z-index: 2001;
        padding: 10%;
        left: 0;
        text-align: center;
        width: 100%;
        background-color: rgba(0, 0, 0, 0.4);
        top: 50%;
        transform: translateY(-50%);
        color: #fff;
        display:none;
    }
    .user_input{
        margin-top: 5px !important;
        width: 90vw !important;
    }

</style>

最佳答案

为您的输入定义专用属性。

 data () {
    return {
             ... // rest remains same
                currentUser
            }
        },

将您输入的 v-model 绑定(bind)到 currentUser

<input name="adduser"
       id="adduser" 
       @input="addParticipation(user.upn)"
       class="form-control user_input" 
       type="text" 
       v-model="currentUser" 
       list="manual" placeholder="Manually add an attendee">

在您想要清除数据的地方,为 currentUser 分配一个空值。 因此,将:$('#adduser').val(''); 更改为:this.currentUser=''

并且停止使用 JQuery。如果您使用 Vue、Angular 或 React 类型的框架,它们有自己的处理方式。使用它们而不是创建额外的或遗留的依赖项。

如果您使用v-for,请在可能的情况下提供v-bind:key

<datalist id="manual">
    <option v-for="user in users"
            v-bind:value="user.upn"
            v-bind:key="user.upn">
           {{user.firstname}} {{user.lastname}} (Year {{user.year}})
   </option>
</datalist>

关于javascript - VueJS - 数据列表输入未清除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60003741/

相关文章:

jquery - Jquery Nivo Slider 转换如何工作?

javascript - 如何在递归组件中出现 "Avoid mutating a prop directly"

javascript - 在使用 Flask 时也使用大括号作为 Vue.js 中的值

css - 导入样式文件没有作用域?

javascript - 单击而不是悬停访问无限下拉菜单

javascript - 以编程方式构造在 if() 语句中使用的条件

javascript - 实际拖动的 native HTML 5 拖放?

html - 如何对齐div中的复选框

javascript - 如何将按钮放在右下角和所有内容之上?

php - 很简单的购物车,移除按钮