js頁面對象控制

來源:互聯網
上載者:User
**
 * 編輯區表格版面配置樣式初始化
 * @param objId 編輯地區table元素的id
 * @return
 */
function initEditArea(objId) {
  var table = $(objId);
  var rows = table.rows;
  for(var i=0; i<rows.length; i++) {
    cells = rows[i].cells;
    for(var j=0; j<cells.length; j++) {
      if(j%2 == 0) {
        cells[j].style.backgroundColor="#E7F1FE";
      } else {
        cells[j].style.backgroundColor="#F8FBFF";
      }
    }
  }
}
/**
 * 給select插入預設值
 * @param selectTag select元素對象
 * @param desc 預設值的描述
 */
function insertDefaultOption(selectTag,desc,sValue) {
  if(typeof sValue == "undefined" || sValue == null) {
    sValue = "";
  }
  if(desc == null) desc = "";
  var obj = eval(selectTag);
  var newOption = document.createElement('option');
  newOption.innerHTML=desc;
  newOption.setAttribute("value",sValue);
  obj.options[0].parentNode.insertBefore(newOption,obj.options[0]);
  obj.selectedIndex = 0;
}

/**
 * 清空form
 * @param oForm為form對象
 */
function resetForm(oForm){
  var oForm = eval(oForm);
  for(var i = 0; i < oForm.elements.length; i++){
    var e = oForm.elements[i];
    if( e.type == null ) continue;
    if( e.tagName.toUpperCase() == "INPUT" ){
      if( e.type.toUpperCase() == "TEXT" || e.type.toUpperCase() == "PASSWORD" )  {
        e.value = "";
      }
      if( e.type.toUpperCase() == "RADIO" ) e.checked = false;
      if( e.type.toUpperCase() == "CHECKBOX" ) e.checked = false;
      if( e.type.toUpperCase() == "HIDDEN" ){
        if( typeof(e.setEnable) == "function" ){
          e.setValue(getTime());
        }
      }
    }
    if( e.tagName.toUpperCase() == "SELECT" ) {
      try{
      if(e.options.length > 0) //e.options[0].selected = true;
        e.value = "";
      } catch(ex){}
    }
    if( e.tagName.toUpperCase() == "TEXTAREA" ) {
        e.value = "";
    }
  }
}

/**
 * 設控制項是否為唯讀
 * @param obj 控制項
 * @param isRead true為唯讀,false為可編輯
 * @return
 */
function setInputEnable(obj, isRead){
  try{
      if(obj.tagName.toUpperCase()=="SELECT"
         || obj.type.toUpperCase()=="CHECKBOX")
        obj.disabled = isRead;
      else
        obj.readOnly = isRead;

    if (isRead){      //不可編輯樣式
       obj.className="input-gray";
    }else{      //可編輯樣式
      obj.className="";
    }
  }catch(ex){
        alert("setInputEnable()===>"+ex.description);
  }
}

/**
 * 檢查輸入欄位是否是固定長度的數字
 * obj    input對象
 * length 固定的數字長度
 * desc   欄位名稱
 */
function checkNumOfGivenLength(obj, length, desc) {
  var str = obj.value;
  if(str.length == 0) {
    return true;
  }
  var pattern = /^-?/d+$/;
  if(str.match(pattern) == null || str.length != length) {
    showMsg("“"+desc+"”必須為一個長度為"+length+"的數字,請修改!");
    return false;
  }
  return true;
}

/**
 * 檢查輸入欄位是否合法
 *
 * obj    input對象
 * name   欄位名稱
 * isNull 是否可為空白
 * length 長度
 * isNum  是否為數字
 * iMin   最小值
 * iMax   最大值
 */
function checkField(obj, name, isNull, length, isNum, iMin, iMax) {
    if(arguments.length < 5) {
        throw new Error("傳入的參數太少,至少需要5個參數。");
    }
    else if(isNum && arguments.length != 7) {
        throw new Error("傳入的參數太少,需要7個參數。");
    }
    var value  = obj.value;
    if(!isNull && value.length == 0){
        showMsg("“"+name+"”不可為空白!");
        obj.focus();
        return false;
    }
    if(isNum){
        var pattern = /^-?/d+$/;
        if(value.match(pattern) == null) {
            showMsg("“"+name+"”必須為一個數字,請修改!");
            obj.focus();
            return false;
        } else {
            if ((typeof(iMin)=="undefined") || (typeof(iMax)=="undefined")){
              return true;
            }
            if ((value <= iMax) && (value >= iMin)){
              return true;
            }else{
              showMsg("“"+name+"”必須介於"+iMin+"與"+iMax+"之間!");
              obj.focus();
              return false;
            }
        }
    }
    if(getStrLength(value) > length) {
        var sDesc = "";
        if(!isNum) {
            sDesc = "(一個漢字的長度為2)";
        }
        showMsg("“"+name+"”長度不能超過"+length+"!"+sDesc);
        obj.focus();
        return false;
    }
    return true;
}

/**
 * 獲得字串長度,一個漢字兩個
 */
function getStrLength(strTemp){
  var i,sum;
  sum=0;
  for(i=0;i<strTemp.length;i++){
    if ((strTemp.charCodeAt(i)>=0) && (strTemp.charCodeAt(i)<=255))
      sum=sum+1;
    else
      sum=sum+2;
  }
  return sum;

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.