JS中表单的使用小结


Posted in Javascript onJanuary 11, 2014

1.javascript刷新页面的方法
     
window.location.reload();

使用window.open()弹出的弹出窗口,刷新父窗口
    window.opener.location.reload()

使用window.showDialog弹出的模式窗口
    window.dialogArguments.location.reload();

2.javascript弹出窗口的两种实现方式 ---下面给两个弹出屏幕居中窗口的例子
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 = '';
    document.body.insertAdjacentHTML("beforeEnd", str);
     document.all.noTipClose.Click(); 
   } else {
      window.opener =null; window.close();
   }
  }else{ 
  window.close() 
  }
}

5、定时关闭弹出的窗口---设置/清除定时器
scriptlanguage="JavaScript" 
!-- 
functioncloseit(){ 
setTimeout("self.close()",100000)//单位是毫秒,这里是100秒 
setInterval("self.close()",100000)
window.clearTimeout(me.timer); 
window.clearInterval(me.timer); 
/script

6.javascript弹出子窗口中传值---通过url传值
<script language="javascript" type="text/javascript">
function fn_modify(pid){
    var ModifyInfo=new Object();
 window.showModalDialog("modify_main.asp?pid="+pid,ModifyInfo,"dialogHeight:180px;dialogWidth:300px;dialogLeft:;dialogTop:;resizable:off;center:on;help:off;scroll:off;status:off")
 Reload();
}
function Reload(){location.href="abc.asp";}
</SCRIPT> 
<a href="abc.asp" onClick="fn_modify('这是值')">单击</a>

7.js隐藏/显示表单
document.all("id").style.display=="none";//隐藏
document.all("id").style.display=="";//显示
document.getElementById("bt").style.display=="none"
document.getElementById("bt").style.display==""
id为table,input 的id

8.js控制表单元素有效/失效
document.getElementById("bt").disabled=true;
document.all("Submit1").disabled=true;//失效
document.all("Submit1").disabled=false;//有效

设置/获取元素的值
document.getElementById("labTitle").innerHTML="IP模式";//设置值
document.getElementById("labTitle").innerHTML//获取值
labTitle 为div,span,table的id

实例1:

<input id="mytext" type="text" value="我是不能用的">
<input type="button" value="disabled" onClick="javascript: document.all.mytext.disabled='false'">
<input type="button" value="enable" onClick="javascript: document.all.mytext.removeAttribute('disabled')">

实例2:

<input id="mytext" type="text" value="我是能用的">
<input type="button" value="disable" onClick="if (mytext.disabled==false){ mytext.disabled=true;mytext.value='我是不能用的';this.value='enable'} else { mytext.disabled=false;mytext.value='我是能用的';this.value='disable'}">

9.页面通过函数提交表单的方法

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

10.遍历radio方法
<input id="mode1" type="radio"    name="workMode" value="1" checked>
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
<select id="ddlProvince" name="ddlProvince" onchange="cityResult()">
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("#");
      var sZoneID=idpArray[0];
      var sProvince=idpArray[1];
      prov.options.add(new Option(sProvince,sZoneID));
    }

12.页面中使用prototype ajax提交数据的实现方式(java)

一步:在<head></head>中添加以下js文件链接

<head>
<script language="JavaScript" src="/NDHotel/js/prototype-1.6.js"></script>
</head>

二步:把prototype-1.6.js文件放到/NDHotel/js/指定的目录中

三步:在<script type="text/javascript"></script>中声明以下调用函数

<script type="text/javascript">
function editIpSegment(){
 var url='/NDHotel/ipsegmentset.do?method=roomChangeNotice';
 var pars = 'startip='+startip+'&endip='+endip+'&lindex='+lindex;new Ajax.Request( url, {method: 'get', parameters: pars, asynchronous:false,onComplete:editResult});
}

function editResult(result){
    var returnStr = result.responseText;
    if(returnStr =='fail'){
      alert("");
      return false;  
    }
}
</script>

四步:实现后台调用
 public ActionForward roomChangeNotice(ActionMapping mapping,
   ActionForm form, HttpServletRequest request,
   HttpServletResponse response) throws Exception {
  String result = "";
  PrintWriter pw = RainPrintWriter.getPrintWriter(response);
  try {
   NotifyServiceTwo.sendMessage(4, 0);
   result = "success";
  } catch (Exception e) {
   logger.error("roomChangeNotice" + e);
  }
  pw.write(result);
  pw.close();
  return null;
 }

13.js中获取表单的值的方式:
document.getElementById("lindex").value
document.all.lindex.value//lindex在页面中必须是唯一的
//设置获取焦点
document.getElementById("lindex").focus()
document.all.startip.focus()
//设置失去焦点
document.getElementById("lindex").blur()
document.all.startip.blur()

14.动态添加/删除表格中的行
<table width="100%"  id="tdSearch" name="tdSearch" cellpadding="0" cellspacing="0" align="center">
</table>
//动态生成table的行
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 width='200' value='"+value1+"' onchange='changeip("+autoId+")' type='text' id='bIPFrom_"+autoId+"'>-";
 newRow.insertCell(); 
 newRow.cells[1].innerHTML = "<input width='200' value='"+value2+"' type='text' id='bIPTo_"+autoId+"'> ";      
   var cell2 = newRow.insertCell(); 
 cell2.innerHTML = "<input  class='btn_1word' type='button' class='HQ_BUTTON' value='-' onClick=removeRow('" + newRow.id + "')>";
       cell2.setAttribute("class", "yellowCell2"); 
 autoId=autoId+1;
}
function removeRow(rowId){
 var trRow = document.getElementById(rowId);
  //alert(trRow);
 //if(rowId!="row_0"){
  trRow.removeNode(true);
 //}
}

15. 集合
//显示导入进度条
document.all("btnImport").disabled=true;
document.all("DataGrid_WaitDiv").style.left=100;
document.all("DataGrid_WaitDiv").style.top=295;
document.all("DataGrid_WaitDiv").style.display = "";
form1.action="/NDHotel/jsp/systemset/roomSet/uploadFile.jsp";
form1.submit();
16.新建一个窗口
function layer1AddGroup() {
var url='/NDHotel/jsp/systemset/roomSet/addGroup.jsp';
var newwin=window.showModalDialog(url,window,"dialogWidth=470px;dialogHeight=400px;scroll=yes;status=no;help=no;");
}
//刷新父页面
function roomMainLeftRightFrame(){
var layer='<%=layer%>';
window.parent.parent.frames('view').location.href="/NDHotel/troom.do?method=roomSetLeftMenu&layer="+layer; 
}

17.设置文本框只读属性/设置文本框的颜色/设置radio选中
document.all("txt_AutoTime").readOnly=true;
document.all("txt_AutoTime").style.backgroundColor="#d0d0d0";
runParamSetForm.radNotForcibly.checked=true;

//IP地址验证
function ipCheck(ipValue){
 var reg = /^/d{1,3}(/./d{1,3}){3}$/;
 if(ipValue != ""){
  if (reg.test(ipValue)){
   var ary = ipValue.split('.');
   for(key in ary){
    if (parseInt(ary[key]) > 255 )
     return false;
   }
   return true;
  }else
 return false; 
 }else
 return true; 
}
Javascript 相关文章推荐
javascript 进度条 实现代码
Jul 30 Javascript
为jquery.ui.dialog 增加“自动记住关闭时的位置”的功能
Nov 24 Javascript
JS的document.all函数使用示例
Dec 30 Javascript
让jQuery Mobile不显示讨厌loading界面的方法
Feb 19 Javascript
jQuery插件开发精品教程(让你的jQuery更上一个台阶)
Nov 07 Javascript
Node.js的Koa框架上手及MySQL操作指南
Jun 13 Javascript
jQuery包裹节点用法完整示例
Sep 13 Javascript
JavaScript制作简易计算器(不用eval)
Feb 05 Javascript
vue实现图片滚动的示例代码(类似走马灯效果)
Mar 03 Javascript
JS回调函数原理与用法详解【附PHP回调函数】
Jul 20 Javascript
详解JS预解析原理
Jun 16 Javascript
JS 基本概念详细介绍
Oct 16 Javascript
js opener的使用详解
Jan 11 #Javascript
JS获取下拉列表所选中的TEXT和Value的实现代码
Jan 11 #Javascript
JS,Jquery获取select,dropdownlist,checkbox下拉列表框的值(示例代码)
Jan 11 #Javascript
js获取下拉列表框中的value和text的值示例代码
Jan 11 #Javascript
两个select多选模式的选项相互移动(示例代码)
Jan 11 #Javascript
利用javascript打开模态对话框(示例代码)
Jan 11 #Javascript
showModalDialog模态对话框的使用详解以及浏览器兼容
Jan 11 #Javascript
You might like
thinkphp使用phpmailer发送邮件的方法
2014/11/24 PHP
深入理解PHP中的count函数
2016/05/31 PHP
laravel-admin 中列表筛选方法
2019/10/03 PHP
IE 上下滚动展示模仿Marquee机制
2009/12/20 Javascript
javascript中检测变量的类型的代码
2010/12/28 Javascript
JQuery学习笔录 简单的JQuery
2012/04/09 Javascript
js 实现菜单上下显示附效果图
2013/11/21 Javascript
js获取客户端外网ip的简单实例
2013/11/21 Javascript
深入理解JavaScript是如何实现继承的
2013/12/12 Javascript
简单的邮箱登陆的提示效果类似于yahoo邮箱
2014/02/26 Javascript
jquery事件的ready()方法使用详解
2015/11/11 Javascript
jquery实现横向图片轮播特效代码分享
2015/11/19 Javascript
基于jQuery实现弹出可关闭遮罩提示框实例代码
2016/07/18 Javascript
微信小程序 input输入框控件详解及实例(多种示例)
2016/12/14 Javascript
简单的jQuery拖拽排序效果的实现(增强动态)
2017/02/09 Javascript
js中编码函数:escape,encodeURI与encodeURIComponent详解
2017/03/21 Javascript
详解vue项目构建与实战
2017/06/27 Javascript
详解vue父子组件间传值(props)
2017/06/29 Javascript
AngularJS中使用three.js的实例详解
2017/07/21 Javascript
vue2.0移动端滑动事件vue-touch的实例代码
2018/11/27 Javascript
vue项目中企业微信使用js-sdk时config和agentConfig配置方式详解
2020/12/15 Vue.js
python算法演练_One Rule 算法(详解)
2017/05/17 Python
Pandas 数据处理,数据清洗详解
2018/07/10 Python
Python检查ping终端的方法
2019/01/26 Python
基于python2.7实现图形密码生成器的实例代码
2019/11/05 Python
django 外键创建注意事项说明
2020/05/20 Python
详解Python IO口多路复用
2020/06/17 Python
html5利用canvas绘画二级树形结构图的示例
2017/09/27 HTML / CSS
美国校园市场:OCM
2017/06/08 全球购物
美国婴儿用品及配件购买网站:Munchkin
2019/04/03 全球购物
比利时的在线灯具店:Lampen24.be
2019/07/01 全球购物
互联网创业计划书写作技巧攻略
2014/03/23 职场文书
网站创业计划书
2014/04/30 职场文书
优秀党支部书记事迹材料
2014/05/29 职场文书
运动会演讲稿200字
2014/08/25 职场文书
服务器间如何实现文件共享
2022/05/20 Servers