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
、選中去除文本框文字,離開后顯示原有文字:
?<input name="key" type="text" id="key" value="關(guān)鍵詞" size="30" ??
? ? ? ? ? onmouseover=this.focus();this.select(); ?
? ? ? ? ? onclick="if(value==defaultValue){value='';this.style.color='#000'}" ??
? ? ? ? ? onBlur="if(!value){value=defaultValue;this.style.color='#999'}" style="color:#999" />
2、選中后方可編輯:
<input type="checkbox" name="tpbox" value="1" onclick="if(this.checked) {txtNo.disabled=false}else{txtNo.disabled=true}">你一定要幸福,我會(huì)好好的!
你的姓名:<input type="text" name="txtNo" size="20" value="選中前面的選項(xiàng)方可編輯" disabled>
3、點(diǎn)擊鏈接后方可編輯:
<a href="#" onclick="username.readOnly=false;alert('你好,歡迎使用!')">先點(diǎn)擊我哦!</a>
你的姓名:<input id="username" value="--請(qǐng)輸入--" size="30" readOnly>
4、輸入框從中間輸入:從中間輸入:
<input type="text" name="mid"style="text-align:center;">
5、輸入框變色:輸入框改變變色:
<input type="text" size="20" style="background-color:#FFFFFF"
? ? ? ? ? ? onfocus="style.backgroundColor='#FFFF00'"
? ? ? ? ? ? onblur="style.backgroundColor='#FFFFFF'">
6、輸入框只能輸入數(shù)字(用的是正則表達(dá)式):你 的年齡:
<input onkeyup="value=value.replace(/[^\d]/g,'') "
? ? ? ? onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">
7、輸入框只能輸入中文(用的是正則表達(dá)式):你的中文名:
<input onkeyup="value=value.replace(/[ -~]/g,'')" onkeydown="if(event.keyCode==13)event.keyCode=9">
8、只能輸入英文和數(shù)字(用的是正則表達(dá)式):
你 的昵稱:<input onkeyup="value=value.replace(/[\W]/g,'') "
? ? ? ? onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
? ? ? ? onkeydown="if(event.keyCode==13)event.keyCode=9">
9、輸入框不能編輯,但表單可以獲得輸入框內(nèi)的值:
<input type="text" value="afreon" onclick="alert('總和不能編輯!');" onfocus="this.blur()" />
<input type="text" value="afreon" onclick="alert(this.value);" readonly />
<input value="不可修改" ?readonly= "true" type="text"/>//:字體顏色為黑體
10、輸入框不能編輯,并且表單不能獲得輸入框內(nèi)的值
<input value="不可修改" disabled="disabled" ?type="text"/>//:字體顏色為灰體
11、輸入框禁止輸入法:
<input onpaste="return false" style="ime-mode:disabled">
現(xiàn)效果
代碼:
前端開發(fā)中,經(jīng)常需要獲取用戶輸入框中的內(nèi)容。本文將介紹幾種常用的JavaScript方法來獲取輸入框內(nèi)容,幫助您輕松應(yīng)對(duì)獲取輸入框內(nèi)容的需求。
let input = document.getElementById("myInput");
let content = input.value;
console.log(content);
通過 getElementById() 方法獲取輸入框的 DOM 元素。
使用 value 屬性獲取輸入框的文本內(nèi)容。
let input = document.querySelector("#myInput");
let content = input.value;
console.log(content);
使用 querySelector() 方法獲取輸入框的 DOM 元素。
使用 value 屬性獲取輸入框的文本內(nèi)容。
let input = document.getElementById("myInput");
input.onchange = function() {
let content = input.value;
console.log(content);
}
通過 onchange 事件在輸入框內(nèi)容變化時(shí)觸發(fā)函數(shù)。
在事件處理函數(shù)中,使用 value 屬性獲取輸入框的新文本內(nèi)容。
let input = document.getElementById("myInput");
input.oninput = function() {
let content = input.value;
console.log(content);
}
通過 oninput 事件實(shí)時(shí)獲取輸入框內(nèi)容的變化。
在事件處理函數(shù)中,使用 value 屬性獲取輸入框的新文本內(nèi)容。
以上是JavaScript獲取輸入框內(nèi)容的幾種常用方法。您可以根據(jù)具體的需求和場(chǎng)景選擇適合的方法。在實(shí)際開發(fā)中,根據(jù)頁面結(jié)構(gòu)和交互需求選擇最合適的方式獲取輸入框內(nèi)容,以提升用戶體驗(yàn)。
本文僅供參考,請(qǐng)根據(jù)實(shí)際情況進(jìn)行使用。
在獲取輸入框內(nèi)容時(shí),可以通過驗(yàn)證和過濾確保數(shù)據(jù)的安全性。
對(duì)于多個(gè)輸入框,可以使用不同的方法來獲取各自的內(nèi)容。
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。