JS中表单的使⽤⼩结
1.javascript刷新页⾯的⽅法
load();
使⽤window.open()弹出的弹出窗⼝,刷新⽗窗⼝
window.load()
使⽤window.showDialog弹出的模式窗⼝
window.load();
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);
TipClose.Click();
} else {
window.opener =null; window.close();
}jsarray删除元素
}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=="";//显⽰
id为table,input 的id
8.js控制表单元素有效/失效
document.all("Submit1").disabled=true;//失效
document.all("Submit1").disabled=false;//有效
设置/获取元素的值
labTitle 为div,span,table的id
复制代码代码如下:
<input id="mytext" type="text" value="我是不能⽤的">
<input type="button" value="disabled" onClick="javascript: disabled='false'">
<input type="button" value="enable" onClick="javascript: veAttribute('disabled')">
复制代码代码如下:
<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 ElementsByName("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 ElementById("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 = sponseText;
if(returnStr =='fail'){
alert("");
return false;
}
}
</script>
四步:实现后台调⽤
复制代码代码如下:
public ActionForward roomChangeNotice(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String result = "";
PrintWriter pw = PrintWriter(response);
try {
NotifyServiceTwo.sendMessage(4, 0);
result = "success";
} catch (Exception e) {
<("roomChangeNotice" + e);
}
pw.write(result);
pw.close();
return null;
}
13.js中获取表单的值的⽅式:
复制代码代码如下:
document.all.lindex.value//lindex在页⾯中必须是唯⼀的
//设置获取焦点
document.all.startip.focus()
//设置失去焦点
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 ElementById("tdSearch");
highQuery.insertRow();
var newRow = ws.length - 1];
newRow.id = "row_" + autoId;
newRow.insertCell();
id='bIPFrom_"+autoId+"'>-";
newRow.insertCell();
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 = ElementById(rowId);
//alert(trRow);
//if(rowId!="row_0"){
//}
}
15. 集合
复制代码代码如下:
//显⽰导⼊进度条
document.all("btnImport").disabled=true;
document.all("DataGrid_WaitDiv").style.left=100;
document.all("DataGrid_WaitDiv").p=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 (st(ipValue)){
var ary = ipValue.split('.');
for(key in ary){
if (parseInt(ary[key]) > 255 )
return false;
}
return true;
}else
return false;
}else
return true;
}