###### tags: `ProgrammingLanguage` # Javascript ## 目錄 :::spoiler isChecked ```isChecked $("#as_exyn").click(function () { isChecked = $(this).is(":checked"); }); ``` ::: --- :::spoiler 日期民國西元轉換 ``` var btn = document.getElementById("myBtn"); btn.addEventListener("click", getValue); function getValue() { //民國轉西元 var as_b_date = $("#as_b_date").val(); var arrDay = as_b_date.split("/"); var strDate = (parseInt(arrDay[0]) + 1911).toString() + "/" + arrDay[1].toString() + "/" + arrDay[2].toString(); as_b_date = Date.parse(strDate); var as_e_date = $("#as_e_date").val(); var arrDay = as_e_date.split("/"); var strDate = (parseInt(arrDay[0]) + 1911).toString() + "/" + arrDay[1].toString() + "/" + arrDay[2].toString(); as_e_date = Date.parse(strDate); //當天日期 var Today = Date.parse((new Date()).toDateString()); //開發文件規則 查詢日期不能超過當天日期 if (as_b_date > Today) { $("#as_b_date")[0].pattern = true; $("#as_b_date")[0].title = "查詢日期不能超過當天日期"; } else { $("#as_b_date")[0].pattern = "(0[0-9][0-9]|1[0-9][0-9]|2[0-9][0-9])[/](0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])"; } if (as_e_date > Today) { $("#as_e_date")[0].pattern = true; $("#as_e_date")[0].title = "查詢日期不能超過當天日期"; } else { $("#as_e_date")[0].pattern = "(0[0-9][0-9]|1[0-9][0-9]|2[0-9][0-9])[/](0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])"; } } ``` ::: --- ::: spoiler 判斷空值 function isNull( str ){ if ( str == "" ) return true; var regu = "^[ ] $"; var re = new RegExp(regu); return re.test(str); } ::: --- ::: spoiler 報表js ``` $(document).ready(function(){ $('[id^=btn]').on('click',function(){ let url = '<?php echo $rep_ip; ?>'; let report = $('#report').val() ?? $('#reportxls').val(); let format = ($(this).val() == "執行報表" ? "PDF" : "XLS"); url += '?report=' + report; url += '&SchoolNo=' + '<?php echo $SchoolNo; ?>'; url += '&format=' + format; for(let i = 1; i <= $('[id^=param]').length; i++ ){ url += '&param' + i + '=' + ($('#param' + i).val() ?? ''); } window.open(url); }); }); ``` ::: --- ::: spoiler nth-child ```加入元素 $(".form-horizontal .form-group:nth-child(7)").before('<div class="form-group"><lable class="control-label col-md-2">建物:</lable><div class="col-md-10"><select name="buna" id="buna" style="width: 20%; height: 34px;" class="form-control input"></select></div></div>'); ``` ::: --- :::spoiler 取得值$('.ListRow') ``` 取得值 $('.ListRow').attr("data-id") $('.ListRow')[0].className ``` ::: --- :::spoiler DOMPoint window.DOMPoint(); ::: --- :::spoiler clone() //$(".tab-content:nth-child(2n+3):nth-child(2n+1)").clone().appendTo($(".tab-content:nth-child(2n+3):nth-child(2n+1)")); //$(".tab-content .tab-content").clone().appendTo($(".tab-content .tab-content")); $(function () { let as_b_syear = $("#as_b_syear").val(); let as_b_sem = $("#as_b_sem").val(); let as_e_syear = $("#as_e_syear").val(); let as_e_sem = $("#as_e_sem").val(); let src1 = "/cash/cash_010_copy_Frame/index?fid=" + as_b_syear + "&fid2=" + as_b_sem + "&fid3=" + as_e_syear + "&fid4=" + as_e_sem; $("#menu0 #frame")[0].src = src1; //clone tab-content元素 $(".tab-content").clone().appendTo($(".tab-content")); $(".tab-content #menu0")[1].id = "menu1"; $(".tab-content #tab1")[1].id = "tab2" let src2 = "/cash/cash_060_copy_Frame/index?fid=" + as_b_syear + "&fid2=" + as_b_sem + "&fid3=" + as_e_syear + "&fid4=" + as_e_sem + "&fid5=3"; $("#menu1 #frame")[0].src = src2; $("#tab2").text("2"); }); ::: --- :::spoiler 保留下拉的值 ` //保留下拉的值 var selwhrValue = localStorage.getItem("selwhrValue"); if (selwhrValue != null) $("select[name=selwhr]").val(selwhrValue); $("select[name=selwhr]").on("change", function () { localStorage.setItem("selwhrValue", $(this).val()); }); ` ::: --- :::spoiler 多元素事件 ``` $("[name='ad_dev[]']").each(function (i, v) { $(v).change(function () { $("[name='ad[]']:eq(" + i + "")").attr("readonly", $(v)[0].checked); }); }); ``` ::: --- :::spoiler 連續值 ``` //獎狀號(起值)箭頭功能 function fillIn2() { let num = $("#schnum").val(); //輸入值 let strNumLength = num.length; //輸入值長度 $.each($("[name='dt_schnum[]']"), function (i, v) { if (this.value == "" || this.value == null) { //若欄位為空 if (num == "") //輸入值為空帶入空 this.value = num; else { $.each($("[name='dt_schnum[]']"), function (ii, vv) { num = paddingLeft(num, strNumLength); //比對前先不足位數補0 if (num == $(vv).val()) { //若重複 num = Number(num) + 1; //當前值加1 i--; //索引值減1(使其繼續跑當前欄位) } return true; //重新進迴圈,不往下執行 }); this.value = paddingLeft(num, strNumLength); //沒重複將當前值帶入 num = Number(num) + 1; //並將當前值再加1 } } }); } ``` ::: --- ::: spoiler 判斷日期重複 ``` $(function () { $("[name$='_day[]'").each(function (i, v) { $(this).blur(function () { let v = $(this).val(); $("[name$='_day[]'").each(function (j, vv) { if(v == vv.value && i != j){ alert(vv.value + " 日期重複"); return; } }) }) }) }) ``` :::