因爲我的”.ui-checkbox-new”,是頁面新添加的元素,所以在綁定事件的時候,就用on。在在用on綁定事件的時候,這個click事件會觸發兩次。用alert函數測試的時候,“1–11–1–11”。
https://blog.csdn.net/JaneLittle/article/details/60778352
在網上尋找解決方法:在代碼中加入return false; 阻止冒泡事件後,程序就可以正常運行,不會執行兩次了。
$("#today").on("click",".ui-checkbox-new",function(){
var indexList = $(this).index();
alert(1);
console.log(indexList +" "+ localStorage.getItem('workDone'+indexList) +" ");
// $(this).remove();
localStorage.removeItem('workDone'+indexList);
localStorage.setItem("workDone"+indexList,localStorage.getItem('work'+indexList));
var htmlDone='';
htmlDone ='<div class="ui-checkbox ui-checkbox-new" ><label for="checkbox-done-'+(indexList)+'" class="ui-btn ui-corner-all ui-btn-c ui-btn-icon-left ui-checkbox-on ui-first-child ui-last-child">'+localStorage.getItem('workDone'+indexList)+'</label><input type="checkbox" name="checkbox-a" data-theme="c" id="checkbox-done-'+(indexList)+'"></div>';
alert(11);
$("#hadDone").append(htmlDone);
return false;
//阻止時間冒泡,添加之後能夠正常運行,只運行一次
});