通過下拉框的值來確定輸入框是否可以為空的代碼
更新時間:2011年10月18日 10:40:33 作者:
本節(jié)只是為了做一個記載,實現(xiàn)一個簡單的驗證是否需要預(yù)占下來框有是和否兩個值,當(dāng)選擇“是”時,資源預(yù)占原因輸入框必須輸入值,否則不能跳轉(zhuǎn),當(dāng)選“否”時,不需要驗證資源預(yù)占原因輸入框
有兩個文件a.html和b.html,a.html 即下面的代碼,b.html只是為了驗證通過時候一個跳轉(zhuǎn)頁面,不做任何代碼編寫
因為a.html用的是相對路徑,所以要想程序能正常運行,b.html文件必須和a.html放在同一個目錄下。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<script type="text/javascript">
function verify(){
var isNeed=document.getElementById("isNeed").value;
if(isNeed==1){
var reason=document.getElementById("reason").value;
if(reason==""||reason==null){
alert("預(yù)占資源原因不能為空");
}else{
window.location.href("a.html");
}
}else{
window.location.href("b.html");
}
}
</script>
<body>
是否需要預(yù)占資源
<select id="isNeed">
<option value="1">是</option><!--通過getElementById()的方法來取到下拉框的值,所以 必須有個id,name可以不要-->
<option value="0" selected>否</option>
</select>
<input type="text" id="reason"/>
<input type="button" value="提交" onclick="verify()"/>
</body>
</html>
因為a.html用的是相對路徑,所以要想程序能正常運行,b.html文件必須和a.html放在同一個目錄下。
復(fù)制代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<script type="text/javascript">
function verify(){
var isNeed=document.getElementById("isNeed").value;
if(isNeed==1){
var reason=document.getElementById("reason").value;
if(reason==""||reason==null){
alert("預(yù)占資源原因不能為空");
}else{
window.location.href("a.html");
}
}else{
window.location.href("b.html");
}
}
</script>
<body>
是否需要預(yù)占資源
<select id="isNeed">
<option value="1">是</option><!--通過getElementById()的方法來取到下拉框的值,所以 必須有個id,name可以不要-->
<option value="0" selected>否</option>
</select>
<input type="text" id="reason"/>
<input type="button" value="提交" onclick="verify()"/>
</body>
</html>
相關(guān)文章
js數(shù)據(jù)驗證集合、js email驗證、js url驗證、js長度驗證、js數(shù)字驗證等簡單封裝
js數(shù)據(jù)驗證集合,進行了封裝推薦大家學(xué)習(xí)使用。里面有例子測試代碼。2010-05-05javascript實現(xiàn)上傳圖片前的預(yù)覽(TX的面試題)
javascript實現(xiàn)上傳圖片前的預(yù)覽(TX的面試題)...2007-08-08js模擬權(quán)限選擇實現(xiàn)代碼(select操作)
權(quán)限選擇頁面,選擇、撤回、全部選擇、全部撤回2012-02-02