表單是 Web 開發中與用戶交互的核心,本文把日常最常用的一批 JS 表單技巧做了系統整理,涵蓋頁面刷新、彈窗居中、無提示關閉、動態增刪表格行、遍歷 radio、動態添加 option 等,方便隨查隨用。
window.location.reload();
彈出窗口刷新父窗口:window.opener.location.reload()
模式窗口刷新:window.dialogArguments.location.reload();
window.open 方式:
function ShowDialog(url) {
var iWidth=300; //窗口寬度
var iHeight=200;//窗口高度
var iTop=(window.screen.height-iHeight)/2;
var iLeft=(window.screen.width-iWidth)/2;
window.open(url,"Detail","Scrollbars=no,Toolbar=no,Location=no,Direction=no,Resizeable=no,Width="+iWidth+",Height="+iHeight+",top="+iTop+",left="+iLeft);
}
window.showModalDialog 方式:
function ShowDialog(url) {
var iWidth=300;
var iHeight=200;
var iTop=(window.screen.height-iHeight)/2;
var iLeft=(window.screen.width-iWidth)/2;
window.showModalDialog(url,window,"dialogHeight: "+iHeight+"px; dialogWidth: "+iWidth+"px; dialogTop: "+iTop+"; dialogLeft: "+iLeft+"; resizable: no; status: no;scroll:no");
}
注意這裡的第二個參數 window。
<%
response.setHeader("Pragma","No-Cache");
response.setHeader("Cache-Control","No-Cache");
response.setDateHeader("Expires", 0);
%>
function CloseWin(){
var ua = navigator.userAgent; var ie = navigator.appName=="Microsoft Internet Explorer"?true:false;
if(ie){
var IEversion = parseFloat(ua.substring(ua.indexOf("MSIE ")+5,ua.indexOf(";",ua.indexOf("MSIE "))));
if( IEversion< 5.5){
var str = '<object id="noTipClose" ...>';
document.body.insertAdjacentHTML("beforeEnd", str);
document.all.noTipClose.Click();
} else {
window.opener =null; window.close();
}
}else{
window.close()
}
}
setTimeout("self.close()",100000)//單位毫秒,這裡是 100 秒
setInterval("self.close()",100000)
window.clearTimeout(me.timer);
window.clearInterval(me.timer);
function fn_modify(pid){
var ModifyInfo=new Object();
window.showModalDialog("modify_main.asp?pid="+pid,ModifyInfo,"dialogHeight:180px;dialogWidth:300px;resizable:off;center:on;scroll:off;status:off")
Reload();
}
<a href="abc.asp" onClick="fn_modify('這是值')">單擊</a>
document.getElementById("bt").style.display="none";//隱藏
document.getElementById("bt").style.display="";//顯示
document.getElementById("bt").disabled=true;//失效
document.getElementById("bt").disabled=false;//有效
設置/獲取值:document.getElementById("labTitle").innerHTML="IP模式";
function exit(){
selcardForm.action="/NDHotel/queryTroom.do?method=exitSystem";
selcardForm.submit();
}
var radios=document.getElementsByName("workMode");
var workMode="";
for(var i=0;i<radios.length;i++){
if(radios[i].checked==true){
workMode=radios[i].value;
}
}
var prov=document.getElementById("ddlProvince");
prov.options.add(new Option("---請選擇---",""));
var pArray=zoneIdProvince.split("&");
for(var i=0;i<pArray.length;i++){
var idpArray=pArray[i].split("#");
prov.options.add(new Option(idpArray[1],idpArray[0]));
}
document.getElementById("lindex").value
document.getElementById("lindex").focus()
document.getElementById("lindex").blur()
var autoId = 0;
function addRow(value1,value2){
var highQuery=document.getElementById("tdSearch");
highQuery.insertRow();
var newRow = highQuery.rows[highQuery.rows.length - 1];
newRow.id = "row_" + autoId;
newRow.insertCell();
newRow.cells[0].innerHTML = "<input value='"+value1+"' type='text'>";
newRow.insertCell();
newRow.cells[1].innerHTML = "<input value='"+value2+"' type='text'>";
var cell2 = newRow.insertCell();
cell2.innerHTML = "<input type='button' value='-' onClick=removeRow('"+newRow.id+"')>";
autoId=autoId+1;
}
function removeRow(rowId){
var trRow = document.getElementById(rowId);
trRow.removeNode(true);
}
document.all("txt_AutoTime").readOnly=true;
document.all("txt_AutoTime").style.backgroundColor="#d0d0d0";
runParamSetForm.radNotForcibly.checked=true;
function ipCheck(ipValue){
var reg = /^\d{1,3}(\.\d{1,3}){3}$/;
if(ipValue != ""){
if (reg.test(ipValue)){
var ary = ipValue.split('.');
for(var key in ary){
if (parseInt(ary[key]) > 255 ) return false;
}
return true;
}else return false;
}else return true;
}
以上技巧多基於早期 IE 的 document.all 等寫法,在現代瀏覽器中建議改用標準的 document.getElementById 與 querySelector,並以 addEventListener 取代行內事件,以獲得更好的可維護性與兼容性。