天天看点

关于radio-group取消选择项问题

场景:使用radio组件,删除选中项。使用官网https://uniapp.dcloud.io/component/radio中例子,会出现以下情况。

关于radio-group取消选择项问题
关于radio-group取消选择项问题

左边的图为选中后,右边图为删除选中项后。大家可以看到 执行完删除操作后被选中项还是停留在第三项的位置。想取消这种类似于记忆的功能,即执行完操作后所有选中项清空,如下图所示:

关于radio-group取消选择项问题

f

在删除函数里,把checked的值设置为空就可以了。

 <view class="uni-list">

            <radio-group @change="radioChange">

                <label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="index">

                    <view>

                        <radio :value="index" :checked="index === current" />

                    </view>

                    <view>{{item.name}}</view>

                </label>

            </radio-group>

        </view>

        <button type="primary" @click="del">删除</button>

del:function(){

                if(this.items.length>0 && this.current != '' || this.current==0)

                {

                    this.items.splice(this.current,1);

                    this.current='';  //这里将current值设置为空,就取消了所有选中项

                }

这里有一点需要注意的:在列表选中某一项时,必须将current值和items数组对应的下标设置一致,否则即便将current的值清空,也不会生效的。

 radioChange: function(evt) {            

                this.current=evt.target.value;   //这里相当于index的值             

            }