elementUI中当form中select选择器中的数据发生改变时清空当前选中的状态

在这里插入图片描述
当选择题库时,获取到章和节的列表,章节才能够被选择在这里插入图片描述
现在要实现的效果是当题库发生改变时,章节列表也发生改变,需要清空当前选择项
在这里插入图片描述
如图,题库改变了,章节列表的数据也发生改变了

<el-form ref="form" :model="form" label-width="50px" :rules="rules">
	<el-form-item label="章节:">
		<el-select v-model="form.section" style="margin-right:20px" @change="sectionChange">
		    <el-option
		      v-for="item in section"
		      :key="item.pointId"
		      :label="item.pointName"
		      :value="item.pointId"
		    ></el-option>
		  </el-select>
		  <el-select v-model="form.festival" @change="festivalChange">
		    <el-option
		      v-for="item in festival"
		      :key="item.pointId"
		      :label="item.pointName"
		      :value="item.pointId"
		    ></el-option>
		  </el-select>
	</el-form-item>
</el-form>

这是基本数据结构

export default {
	data() {
		return {
			form: {
		        section: null,
		        festival: null
	      	},
	      section: [],
     	  festival: [],
     },
     watch: {
	    section: function(newValue) {
	      this.section = newValue;
	      this.form.section = null;
	      this.form.festival = null;
	 }
  },
},

我监听this.section中是否有新数据,如果有就让this.section=新数据,然后清空表单中的旧数据.

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
根据您提供的代码片段,这是一个 `<select>` 元素的示例,具有 id 为 "post",class 为 "form-control select2-multiple",并设置了 multiple 属性,允许选择多个选项。 您可以使用 JavaScript 或 jQuery 来操作该 `<select>` 元素,并动态添加 `<option>` 元素。下面是一个示例代码,用于向该 `<select>` 元素添加选项: ```javascript // 假设您已经从后台获取到了选项数据,并存储在一个名为 "options" 的数组 var options = [ { value: 'value1', label: 'Label 1' }, { value: 'value2', label: 'Label 2' }, { value: 'value3', label: 'Label 3' }, // ... ]; var selectElement = $('#post'); // 通过选择器获取 <select> 元素 // 清空 <select> 元素的选项 selectElement.empty(); // 根据选项数据生成 <option> 元素并插入到 <select> options.forEach(function(option) { var optionElement = $('<option>') .text(option.label) .val(option.value); selectElement.append(optionElement); }); // 使用 select2 插件进行初始化(如果您已经引入了 select2 插件) selectElement.select2(); ``` 在上述代码,我们假设您已经从后台获取到了选项数据,并将其存储在一个名为 "options" 的数组。然后,通过选择器 `$('#post')` 获取到 `<select>` 元素,并使用 `.empty()` 方法清空的选项。 接下来,在 `options.forEach` 循环,我们根据选项数据生成 `<option>` 元素,并使用 `.append()` 方法将其插入到 `<select>` 元素。 最后,我们使用了 select2 插件对该 `<select>` 元素进行了初始化。请确保已经在页面引入了 select2 插件的相关文件。 希望以上解答对您有帮助。如果您还有其他问题,请随提问。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值