如题,希望选择复选框及输入框输入字符时,能实时显示勾选值及输入框值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="wrap">
<create-search-terms :searchfn="search" :config="checkBoxConfig" v-model="searchText"></create-search-terms>
<div id="show">
勾选值:{{checkBoxConfig.checked}},输入框值:{{searchText}}
</div>
</div>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
let createSearchTerms = {
props: ['searchfn', 'config', 'search'],
template: `
<section id="search">
<div class="search-up clearfix ft-14 color-ff">
<template v-for="item in config">
<input type="checkbox" v-model="item.checked" :id="item.id" :value="item.value"><label :for="item.id">{{item.text}}</label>
</template>
</div>
<div class="search-content clearfix">
<input class="left" type="text" name="searchText" id="searchText" placeholder="请输入要检索的内容" v-model="search">
<span class="left ft-18 color-ff bgc-70" @click="searchfn">检索</span>
</div>
</section>
`
}
Vue.component('create-search-terms', createSearchTerms);
let base_vm = new Vue({
el: '#wrap',
data: {
checkBoxConfig: [{
id: 'flow',
value: 1,
text: '花',
checked: ''
}, {
id: 'picture',
value: 2,
text: '画',
checked: ''
}, {
id: 'build',
value: 3,
text: '建筑',
checked: ''
}, {
id: 'view',
value: 4,
text: '景色',
checked: ''
}, {
id: 'images',
value: 5,
text: '图片',
checked: ''
}, {
id: 'others',
value: 6,
text: '其他',
checked: ''
}],
searchText: '',
},
methods: {
search() {
!$('input[name="searchText"]').val().trim() ? alert('请输入要搜索的内容') : this.searchAjax();
// 阻止冒泡
return false;
},
searchAjax() {
let type = '';
this.checkBoxConfig.forEach((item, index) => {
if (item.checked) {
!type ? type = item.value : type += ',' + item.value;
}
});
}
},
});
</script>
</body>
</html>
你代码里有几处错误
我只是修改了你的前两个错误,然后显示了初始值,让你知道你错在哪里