vue.js刪除動態(tài)綁定的radio的指定項

上圖效果,動態(tài)添加綁定radio選項,然后也可以動態(tài)刪除,右邊編輯器刪除,左邊的視圖也對應的刪除。
視圖代碼 view:
"<ul><li v-for='option in options'>" +
"<input type='radio' :name='groupName'>{{option.text}}" +
"</li></ul>",
數據綁定model.options:
options: [{ id: 1, text: '選項1', checked: false }, { id: 2, text: '選項2', checked: false }]
動態(tài)添加:
vm.options.push({ id: "", text: "新選項", checked: false });
動態(tài)刪除指定radio,我們存儲的是json對象動態(tài)添加到options數組中去,取的時候在每個事件可以傳入$event對象,可以獲取到當前事件源,DOM對象,但是vm.options是個數組,沒法很好的匹配DOM來刪除指定的數組項。
在我們循環(huán)綁定數據的時候一般是 v-for:"item in items" 忘了他還有一個index屬性,當前元素的索引.
這里就簡單了,我們在動態(tài)循環(huán)綁定操作radio數據的時候,把index加上
"<p v-for='(option,optionIndex) in options' @mouseenter='optionEnter($event,optionIndex)' >"
然后根據索引來刪除options的指定選項,就容易了
vm.options.splice(optionIndex, 1);
以上所述是小編給大家介紹的vue.js刪除動態(tài)綁定的radio的指定項,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

