Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
日常開發(fā)中,為了方便數(shù)據(jù)的輸入和輸出,JavaScript提供了一些常用的輸入輸出語(yǔ)句,具體如表1-3所示。
表1常用的輸入輸出語(yǔ)句
類型 | 語(yǔ)句 | 說(shuō)明 |
輸入 | prompt() | 用于在瀏覽器中彈出輸入框,用戶可以輸入內(nèi)容 |
輸出 | alert() | 用于在瀏覽器中彈出警告框 |
document.write() | 用于在網(wǎng)頁(yè)中輸出內(nèi)容 | |
console.log() | 用于在控制臺(tái)中輸出信息 |
接下來(lái)將分別演示document.write0、console.log0和promptO的使用。
1. document.write()
document.write0的輸出內(nèi)容中如果含有HTML標(biāo)簽,會(huì)被瀏覽器解析。下面利用documenL.write0在頁(yè)面中輸出“我是document.write0語(yǔ)句!”,示例代碼如下。
document.write('我是document.write()語(yǔ)句!');
2. console.log()
利用console.log0語(yǔ)句在控制臺(tái)輸出“我是console.log0語(yǔ)句!”,示例代碼如下。
console.log('我是console.log()語(yǔ)句!');
console:.log0的輸出結(jié)果需要在瀏覽器的控制臺(tái)中查看。在Chrome 瀏覽器中按“F12”鍵(或在網(wǎng)頁(yè)空白區(qū)域右擊,在彈出的菜單中選擇“檢查”)啟動(dòng)開發(fā)者工具,然后切換到“Console”(控制臺(tái))面板,即可看到console.log0的輸出結(jié)果。
3.prompt()
利用prompt0語(yǔ)句實(shí)現(xiàn)在頁(yè)面中彈出一個(gè)帶有提示信息的輸入框,示例代碼如下。
prompt(請(qǐng)輸入姓名:');
上述示例代碼運(yùn)行后,將在頁(yè)面中彈出一個(gè)輸人框并提示用戶“請(qǐng)輸人姓名:”提示框。
1、JS支持事件處理業(yè)務(wù)邏輯模型,常用事件
Click 單擊
Dblclick 雙擊
Focus 聚焦
Blur 失去焦點(diǎn)
Mouseover 鼠標(biāo)懸停
Mousemove 鼠標(biāo)移動(dòng)
Mouseout 鼠標(biāo)移開
Change 改變
Load 頁(yè)面加載
Keydown 鍵按下
2、調(diào)用
找到對(duì)應(yīng)的標(biāo)簽,加上屬性 on事件關(guān)鍵字=”JS代碼”。一般調(diào)用的JS代碼邏輯很復(fù)雜,會(huì)用函數(shù)封裝。
3、DOM操作:Document Object Model,文檔對(duì)象模型。網(wǎng)頁(yè)文檔由一個(gè)個(gè)的HTML標(biāo)簽組成,操作網(wǎng)頁(yè)組成元素(標(biāo)簽)。
document.getElementById();
document.getElementsByName();
document.getElementsByTagName();
4、表單提交
方式一:document.getElementById("form1").submit();
方式二:提交按鈕類型設(shè)置為submit,調(diào)用的函數(shù)必須要有一個(gè)返回值,返回false不提交,true提交;調(diào)用函數(shù)時(shí)前面再加個(gè)return 關(guān)鍵字。
5、回車驗(yàn)證的實(shí)現(xiàn)。利用用戶敲擊鍵盤判斷用戶按下的是回車鍵調(diào)用檢測(cè)函數(shù)即可。
6、事件參數(shù):event代表對(duì)應(yīng)的標(biāo)簽執(zhí)行的動(dòng)作。比如文本框按下鍵判斷是不是回車
Onkeydown=”Test(event);”
<script>
function Test(e){
var e=e||window.event;
if(e.keyCode==13){}
}
</script>
鼠標(biāo)懸停,顯示該位置坐標(biāo)(相對(duì))
Onmouseover=”alert(event.clientX+”|”+event.clientY);”
示例源碼:
<html>
<head><title>XXX 用戶注冊(cè)</title></head>
<body style="margin-left:auto; margin-right:auto; text-align:center;">
<form name="form1" id="form1" action="register.php" method="post">
<table>
<caption><h3>用戶注冊(cè)</h3></caption>
<tr><th>用戶名:</th><td><input onKeyDown="EnterCheck(event);" name="loginid" id="loginid" placeholder="請(qǐng)輸入用戶名" /></td></tr>
<tr><th>密碼:</th><td><input name="pwd" id="pwd" type="password" placeholder="請(qǐng)輸入密碼" onKeyDown="EnterCheck(event);" /></td></tr>
<tr><th>確認(rèn)密碼:</th><td><input name="repwd" id="repwd" type="password" placeholder="請(qǐng)輸入確認(rèn)密碼" onKeyDown="EnterCheck(event);" /></td></tr>
<tr><th>昵稱:</th><td><input type="text" name="uname" id="uname" placeholder="請(qǐng)輸入昵稱" onKeyDown="EnterCheck(event);" /></td></tr>
<tr><th>性別:</th><td><input type="radio" checked id="sex" name="sex" />男<input name="sex" type="radio" id="sex" />女
</td></tr>
<tr><th>喜歡的顏色:</th><td>
<select name="ucolor" id="ucolor">
<option selected value="0">==請(qǐng)選擇==</option>
<option value="red">--紅色--</option>
<option value="green">--綠色--</option>
<option value="blue">--藍(lán)色--</option>
</select>
</td></tr>
<tr><th>愛(ài)好:</th><td><input value="playcomputer" name="cbof[]" id="cbof[]" type="checkbox">玩電腦
<input type="checkbox" value="readingbook" name="cbof[]" id="cbof[]" >看書
<input type="checkbox" value="dosporting" name="cbof[]" id="cbof[]">做運(yùn)動(dòng)
</td></tr>
<tr><th>個(gè)人簡(jiǎn)介:</th><td>
<textarea rows="4" name="describe" id="describe" onKeyDown="EnterCheck(event);"></textarea>
</td></tr>
<tr><td></td><td>
<input type="button" value="提交" onClick="return CheckInput();">
<input type="reset" value="重置">
<input type="button" value="取消">
</td></tr>
</table>
</form>
</body>
<script language="javascript" type="text/javascript">
//驗(yàn)證用戶輸入
function CheckInput(){
var result=false;
//獲取用戶名文本框?qū)ο?/p>
var loginid=document.getElementById("loginid");
var pwd=document.getElementById("pwd");
var repwd=document.getElementById("repwd");
var uname=document.getElementById("uname");
var ucolor=document.getElementById("ucolor");
var describe=document.getElementById("describe");
if(CheckIsSafeInputText(loginid,"用戶名")){}
else if(CheckIsSafeInputText(pwd,"密碼")){}
else if(repwd.value!=pwd.value){
alert("密碼與確認(rèn)密碼不一致");
repwd.focus();
}
else if(CheckIsSafeInputText(uname,"昵稱")){}
else if(ucolor.value=="0"){
alert("請(qǐng)至少選擇一個(gè)喜歡的顏色");
ucolor.focus();
}
else if(describe!=null && describe.value.length>256){
alert("個(gè)人簡(jiǎn)介不得超出256個(gè)字符");
describe.focus();
}
else
//document.getElementById("form1").submit();
result=true;
return result;
}
//驗(yàn)證文本框是否合法,非法返回true,合法返回false
function CheckIsSafeInputText(obj,msg){
var result=true;
if(obj==null || obj.value=="")
{
alert(msg+"不得為空");//+為字符串連接符
obj.focus();
}
else if(obj.value.length<4 || obj.value.length>16){
alert(msg+"長(zhǎng)度在4~16個(gè)字符以內(nèi)");
obj.focus();
}
else
result=false;
return result;
}
//回車驗(yàn)證
function EnterCheck(e){
var e=e || window.event;
if(e.keyCode==13)
CheckInput();
}
</script>
</html>
JS的代碼抽出去作為一個(gè)單獨(dú)的文件,命名要對(duì)應(yīng),相應(yīng)的頁(yè)面通過(guò)<script language="javascript" type="text/javascript" src="相對(duì)路徑"></script>來(lái)調(diào)用
現(xiàn)代web開發(fā)中,表單是用戶與網(wǎng)站互動(dòng)的重要方式之一。HTML5為表單提交提供了強(qiáng)大的功能和豐富的輸入類型,讓收集和驗(yàn)證用戶輸入數(shù)據(jù)變得更加容易和安全。本文將詳細(xì)介紹HTML5表單的各個(gè)方面,包括基本結(jié)構(gòu)、輸入類型、驗(yàn)證方法和提交過(guò)程。
HTML表單由<form>標(biāo)簽定義,它可以包含輸入字段、標(biāo)簽、按鈕等元素。一個(gè)基本的表單結(jié)構(gòu)如下所示:
<form action="/submit_form" method="post">
<label for="name">姓名:</label>
<input type="text" id="name" name="name" required>
<label for="email">電子郵箱:</label>
<input type="email" id="email" name="email" required>
<input type="submit" value="提交">
</form>
在這個(gè)例子中,表單有兩個(gè)輸入字段:姓名和電子郵箱。每個(gè)輸入字段都有一個(gè)<label>標(biāo)簽,這不僅有助于用戶理解輸入的內(nèi)容,也有助于屏幕閱讀器等輔助技術(shù)。<form>標(biāo)簽的action屬性定義了數(shù)據(jù)提交到服務(wù)器的URL,method屬性定義了提交數(shù)據(jù)的HTTP方法(通常是post或get)。
HTML5提供了多種輸入類型,以支持不同的數(shù)據(jù)格式和設(shè)備。
<!-- 單行文本 -->
<input type="text" name="username" placeholder="請(qǐng)輸入用戶名" required>
<!-- 密碼 -->
<input type="password" name="password" required minlength="8">
<!-- 郵箱 -->
<input type="email" name="email" required placeholder="example@domain.com">
<!-- 搜索框 -->
<input type="search" name="search" placeholder="搜索...">
<!-- 數(shù)值 -->
<input type="number" name="age" min="18" max="100" step="1" required>
<!-- 滑動(dòng)條 -->
<input type="range" name="volume" min="0" max="100" step="1">
<!-- 電話號(hào)碼 -->
<input type="tel" name="phone" pattern="^\+?\d{0,13}" placeholder="+8613800000000">
<!-- 日期 -->
<input type="date" name="birthdate" required>
<!-- 時(shí)間 -->
<input type="time" name="appointmenttime">
<!-- 日期和時(shí)間 -->
<input type="datetime-local" name="appointmentdatetime">
<!-- 復(fù)選框 -->
<label><input type="checkbox" name="interest" value="coding"> 編程</label>
<label><input type="checkbox" name="interest" value="music"> 音樂(lè)</label>
<!-- 單選按鈕 -->
<label><input type="radio" name="gender" value="male" required> 男性</label>
<label><input type="radio" name="gender" value="female"> 女性</label>
<!-- 下拉選擇 -->
<select name="country" required>
<option value="china">中國(guó)</option>
<option value="usa">美國(guó)</option>
</select>
<!-- 顏色選擇器 -->
<input type="color" name="favcolor" value="#ff0000">
<!-- 文件上傳 -->
<input type="file" name="resume" accept=".pdf,.docx" multiple>
HTML5表單提供了內(nèi)置的驗(yàn)證功能,可以在數(shù)據(jù)提交到服務(wù)器之前進(jìn)行檢查。
<input type="text" name="username" required>
<input type="text" name="zipcode" pattern="\d{5}(-\d{4})?" title="請(qǐng)輸入5位數(shù)的郵政編碼">
<input type="number" name="age" min="18" max="99">
<input type="text" name="username" minlength="4" maxlength="8">
當(dāng)用戶填寫完表單并點(diǎn)擊提交按鈕時(shí),瀏覽器會(huì)自動(dòng)檢查所有輸入字段的有效性。如果所有字段都滿足要求,表單數(shù)據(jù)將被發(fā)送到服務(wù)器。否則,瀏覽器會(huì)顯示錯(cuò)誤信息,并阻止表單提交。
<input type="submit" value="提交">
可以使用JavaScript來(lái)自定義驗(yàn)證或處理提交事件:
document.querySelector('form').addEventListener('submit', function(event) {
// 檢查表單數(shù)據(jù)
if (!this.checkValidity()) {
event.preventDefault(); // 阻止表單提交
// 自定義錯(cuò)誤處理
}
// 可以在這里添加額外的邏輯,比如發(fā)送數(shù)據(jù)到服務(wù)器的Ajax請(qǐng)求
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>表單提交并顯示JSON</title>
</head>
<body>
<!-- 表單定義 -->
<form id="myForm">
<label for="name">姓名:</label>
<input type="text" id="name" name="name">
<br>
<label for="email">電子郵件:</label>
<input type="email" id="email" name="email">
<br>
<input type="button" value="提交" onclick="submitForm()">
</form>
<script>
// JavaScript函數(shù),處理表單提交
function submitForm() {
// 獲取表單元素
var form = document.getElementById('myForm');
// 創(chuàng)建一個(gè)FormData對(duì)象
var formData = new FormData(form);
// 創(chuàng)建一個(gè)空對(duì)象來(lái)存儲(chǔ)表單數(shù)據(jù)
var formObject = {};
// 將FormData轉(zhuǎn)換為普通對(duì)象
formData.forEach(function(value, key){
formObject[key] = value;
});
// 將對(duì)象轉(zhuǎn)換為JSON字符串
var jsonString = JSON.stringify(formObject);
// 彈出包含JSON字符串的對(duì)話框
alert(jsonString);
// 阻止表單的默認(rèn)提交行為
return false;
}
</script>
</body>
</html>
在這個(gè)例子中:
注意,這個(gè)例子中我們使用了type="button"而不是type="submit",因?yàn)槲覀儾幌M韱斡心J(rèn)的提交行為。我們的JavaScript函數(shù)submitForm會(huì)處理所有的邏輯,并且通過(guò)返回false來(lái)阻止默認(rèn)的表單提交。如果你想要使用type="submit",你需要在<form>標(biāo)簽上添加一個(gè)onsubmit="return submitForm()"屬性來(lái)代替按鈕上的onclick事件。
HTML5的表單功能為開發(fā)者提供了強(qiáng)大的工具,以便創(chuàng)建功能豐富、用戶友好且安全的網(wǎng)站。通過(guò)使用HTML5的輸入類型和驗(yàn)證方法,可以確保用戶輸入的數(shù)據(jù)是有效的,同時(shí)提高用戶體驗(yàn)。隨著技術(shù)的不斷進(jìn)步,HTML5表單和相關(guān)API將繼續(xù)發(fā)展,為前端工程師提供更多的可能性。
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。