使用了elementUI的单选按钮,点击按钮时对数据进行校验
<el-radio-group v-model="switch">
<el-radio border :label='1' @click.native='chooseIdClick(1)'>手动选择el-radio>
<el-radio border :label='2' @click.native='chooseIdClick(2)'>手动输入el-radio>
el-radio-group>
chooseIdClick(val){
if(!this.form.prdouct_info.length){
this.$message.warning('请先选择产品~')
return
}
if(val==1){
this.visible1 = true
}else{
this.visible2 = true
}
}
当运行时,点击按钮,发现chooseIdClick方法执行了两次;
与lable标签的默认事件有关
我发现elementUI的el-radio标签在封装过程中是这样的
<label>
<span> <input type='radio' /> span>
<span> 手动选择 span>
label>
方法是添加在label标签上的;
所以用户点击之后的执行是这样的
chooseIdClick方法无需执行两次,仅需要在input点击时触发即可;
<el-radio-group v-model="switch">
<el-radio border :label='1' @click.native='chooseIdClick($event,1)'>手动选择el-radio>
<el-radio border :label='2' @click.native='chooseIdClick($event,2)'>手动输入el-radio>
el-radio-group>
chooseIdClick(e,val){
if(e.target.tagName != 'INPUT'){
return
}
if(!this.form.prdouct_info.length){
this.$message.warning('请先选择产品~')
return
}
if(val==1){
this.visible1 = true
}else{
this.visible2 = true
}
}