DIV CSS 佈局教程網

JavaScript 表單操作大全:刷新、彈窗、取值與動態行
編輯:關於JavaScript     

表單是 Web 開發中與用戶交互的核心,本文把日常最常用的一批 JS 表單技巧做了系統整理,涵蓋頁面刷新、彈窗居中、無提示關閉、動態增刪表格行、遍歷 radio、動態添加 option 等,方便隨查隨用。

1. 刷新頁面的方法

window.location.reload();
彈出窗口刷新父窗口:window.opener.location.reload()
模式窗口刷新:window.dialogArguments.location.reload();

2. 彈出屏幕居中窗口

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。

3. 設置頁面不緩存(JSP)

<%
      response.setHeader("Pragma","No-Cache");
      response.setHeader("Cache-Control","No-Cache");
      response.setDateHeader("Expires", 0);
%>

4. 無提示關閉頁面

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()
 }
}

5. 定時關閉彈出窗口

setTimeout("self.close()",100000)//單位毫秒,這裡是 100 秒
setInterval("self.close()",100000)
window.clearTimeout(me.timer);
window.clearInterval(me.timer);

6. 彈出子窗口並傳值(透過 url)

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>

7. 隱藏/顯示表單

document.getElementById("bt").style.display="none";//隱藏
document.getElementById("bt").style.display="";//顯示

8. 控制表單元素有效/失效

document.getElementById("bt").disabled=true;//失效
document.getElementById("bt").disabled=false;//有效
設置/獲取值:document.getElementById("labTitle").innerHTML="IP模式";

9. 透過函數提交表單

function exit(){
selcardForm.action="/NDHotel/queryTroom.do?method=exitSystem";
selcardForm.submit();
}

10. 遍歷 radio

var radios=document.getElementsByName("workMode");
 var workMode="";
    for(var i=0;i<radios.length;i++){
        if(radios[i].checked==true){
            workMode=radios[i].value;
        }
    }

11. 向 select 動態添加 option

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]));
  }

12. 獲取表單值與焦點控制

document.getElementById("lindex").value
document.getElementById("lindex").focus()
document.getElementById("lindex").blur()

13. 動態添加/刪除表格行

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);
}

14. 設置只讀、改變顏色、radio 選中

document.all("txt_AutoTime").readOnly=true;
document.all("txt_AutoTime").style.backgroundColor="#d0d0d0";
runParamSetForm.radNotForcibly.checked=true;

15. IP 地址驗證

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 取代行內事件,以獲得更好的可維護性與兼容性。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved