超實(shí)用的JavaScript表單代碼段
整理了下比較實(shí)用的Javascript表單代碼段,分享給大家供大家參考,具體內(nèi)容如下
1 多個(gè)window.onload方法
由于onload方法時(shí)在頁(yè)面加載完成后,自動(dòng)調(diào)用的。因此被廣泛的使用,但是弊端是只能實(shí)用onload執(zhí)行一個(gè)方法。下面代碼段,可以保證多個(gè)方法在Onload時(shí)執(zhí)行:
function addLoadEvent(func){
var oldonload = window.onload;
if(typeof window.onload != 'function'){
window.onload = func;
}else{
window.onload = function(){
oldonload();
func();
}
}
}
2 正則表達(dá)式去空格
str.replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g,"");
3 利用正則過(guò)濾中文
str.replace(/[\u4e00-\u9fa5]/g,"");
4 禁止用戶的拷貝和復(fù)制
xxx.oncopy = function(){
return false;
}
xxx.onpaste = function(){
return false;
}
5 限制字符串長(zhǎng)度(區(qū)分中英文)
主要思想:
需要3個(gè)數(shù)據(jù):1 限制輸入的長(zhǎng)度;2 已經(jīng)輸入了多長(zhǎng); 3 截取多少個(gè)字符;
由于JS里面的截取方法不區(qū)分中英文 ,因此
“哈哈哈”.substr(0,2) ----> 哈哈
“haha”.substr(0,2) ---> ha
但是,如果按照編碼一個(gè)漢字應(yīng)該對(duì)應(yīng)2個(gè)字符,一個(gè)字母對(duì)應(yīng)一個(gè)字符。
因此統(tǒng)計(jì) 真實(shí)長(zhǎng)度 時(shí),應(yīng)該是 字符的長(zhǎng)度 + 漢字的個(gè)數(shù)
例如我們限制輸入5個(gè)字符:那么輸入“哈哈”后,就只能輸入一個(gè)h,不能再輸入漢字了。代碼參考如下,可以運(yùn)行一下推敲推敲。
<script type="text/javascript">
var strLen = (function(){//strlLength的功能相同,但是寫法巨麻煩
return function(_str,_model){
_model = _model || "Ch"; //En模式下,中文算作1字符;Ch模式下,中文為2個(gè)字符
var _strLen = _str.length; //獲取字符串長(zhǎng)度
if(_strLen == 0){
return 0;
}else{
var chinese = _str.match(/[\u4e00-\u9fa5]/g); //匹配中文
return _strLen + (chinese && _model == "Ch"?chinese.length:0); //為什么要&&?
}
}
})();
var strLength = function(_str,_model){
_model = _model || "Ch"; //En模式下,中文算作1字符;Ch模式下,中文為2個(gè)字符
var _strLen = _str.length; //獲取字符串長(zhǎng)度
if(_strLen == 0){
return 0;
}else{
var chinese = _str.match(/[\u4e00-\u9fa5]/g); //匹配中文
return _strLen + (chinese && _model == "Ch"?chinese.length:0); //為什么要&&?
}
}
var funcRemainingCharacters = function(){
remainingCharacters = document.getElementById("remainingCharacters");
var clearRemainingCharacters = function(_this){
var _value = _this.value;
var _valueLength = _value.length;
var dataLength = _this.getAttribute("data-length");
var dataModel = _this.getAttribute("data-model");
var subLen = dataLength;
if(dataModel == "Ch"){//僅當(dāng)開(kāi)啟Ch后,才對(duì)重新計(jì)算截取的長(zhǎng)度
_valueLength = strLength(_value,dataModel);
var vv = _value.match(/[\u4e00-\u9fa5]/g); //當(dāng)輸入【哈哈】時(shí),vv是["哈","哈"]數(shù)組
subLen = dataLength - (!vv?0:vv.length);
}
//_valueLength代表總共的字符長(zhǎng)度,比如哈哈哈 為 6
//dataLength是我們定義的限制長(zhǎng)度,比如 5
//subLen是計(jì)算的截取長(zhǎng)度,當(dāng)輸入家具啊
if(_valueLength > dataLength){
_this.value = _value.substr(0,subLen);
}
}
remainingCharacters.onfocus = function(){
clearRemainingCharacters(this);
}
remainingCharacters.onkeyup = function(){
clearRemainingCharacters(this);
}
remainingCharacters.onblur = function(){
clearRemainingCharacters(this);
}
}
addLoadEvent(funcRemainingCharacters);
</script>
全部代碼:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" >
<script type="text/javascript">
function addLoadEvent(func){
var oldonload = window.onload;
if(typeof window.onload != 'function'){
window.onload = func;
}else{
window.onload = function(){
oldonload();
func();
}
}
}
</script>
</head>
<body>
<p class="h3">(支持中英文區(qū)分)限制字符串長(zhǎng)度</p>
<div class="container">
<div class="row">
<div class="col-md-4">
<input type="text" class="form-control" data-length="5" id="remainingCharacters" data-model="Ch">
</div>
</div>
</div>
<script type="text/javascript">
var strLen = (function(){//strlLength的功能相同,但是寫法巨麻煩
return function(_str,_model){
_model = _model || "Ch"; //En模式下,中文算作1字符;Ch模式下,中文為2個(gè)字符
var _strLen = _str.length; //獲取字符串長(zhǎng)度
if(_strLen == 0){
return 0;
}else{
var chinese = _str.match(/[\u4e00-\u9fa5]/g); //匹配中文
return _strLen + (chinese && _model == "Ch"?chinese.length:0); //為什么要&&?
}
}
})();
var strLength = function(_str,_model){
_model = _model || "Ch"; //En模式下,中文算作1字符;Ch模式下,中文為2個(gè)字符
var _strLen = _str.length; //獲取字符串長(zhǎng)度
if(_strLen == 0){
return 0;
}else{
var chinese = _str.match(/[\u4e00-\u9fa5]/g); //匹配中文
return _strLen + (chinese && _model == "Ch"?chinese.length:0); //為什么要&&?
}
}
var funcRemainingCharacters = function(){
remainingCharacters = document.getElementById("remainingCharacters");
var clearRemainingCharacters = function(_this){
var _value = _this.value;
var _valueLength = _value.length;
var dataLength = _this.getAttribute("data-length");
var dataModel = _this.getAttribute("data-model");
var subLen = dataLength;
if(dataModel == "Ch"){//僅當(dāng)開(kāi)啟Ch后,才對(duì)重新計(jì)算截取的長(zhǎng)度
_valueLength = strLength(_value,dataModel);
var vv = _value.match(/[\u4e00-\u9fa5]/g); //當(dāng)輸入【哈哈】時(shí),vv是["哈","哈"]數(shù)組
subLen = dataLength - (!vv?0:vv.length);
}
//_valueLength代表總共的字符長(zhǎng)度,比如哈哈哈 為 6
//dataLength是我們定義的限制長(zhǎng)度,比如 5
//subLen是計(jì)算的截取長(zhǎng)度,當(dāng)輸入家具啊
if(_valueLength > dataLength){
_this.value = _value.substr(0,subLen);
}
}
remainingCharacters.onfocus = function(){
clearRemainingCharacters(this);
}
remainingCharacters.onkeyup = function(){
clearRemainingCharacters(this);
}
remainingCharacters.onblur = function(){
clearRemainingCharacters(this);
}
}
addLoadEvent(funcRemainingCharacters);
</script>
<hr>
<!-- **************************************************************************** -->
</script>
</body>
</html>
6 添加CSS函數(shù)
var setCSS = function(_this,cssOption){
if(!_this || _this.nodeType === 3 || _this.nodeType === 8 || !_this.style){
return;
}
for(var cs in cssOption){
_this.style[cs] = cssOption[cs];
}
return _this;
};
使用時(shí)
setCSS(xxx,{
"position":"relative",
"top":"0px"
});
7 自適應(yīng)文本框
采用scrollHeight復(fù)制給style.height
xxx.style.overflowY = "hidden";
xxx.onkeyup = function(){
xxx.style.height = xxx.scrollHeight+"px";
};
8 復(fù)選框全選、取消和反選
//下面html代碼
<label class="checkbox-inline">
<input type="checkbox" name="actionSelects">讀書
</label>
<label class="checkbox-inline">
<input type="checkbox" name="actionSelects">跑步
</label>
<label class="checkbox-inline">
<input type="checkbox" name="actionSelects">游戲
</label>
<label class="checkbox-inline">
<input type="checkbox" name="actionSelects">游泳
</label>
//下面是js代碼
var targets = document.getElementsByName("actionSelects");
var targetsLen = targets.length;
var i = 0;
document.getElementById("allSelect").onclick = function(){
for(i=0;i<targetsLen;i++){
targets[i].checked = true;
}
} document.getElementById("cancelAllSelect").onclick = function(){
for(i=0;i<targetsLen;i++){
targets[i].checked = false;
}
}
document.getElementById("_select").onclick = function(){
for(i=0;i<targetsLen;i++){
targets[i].checked = !targets[i].checked;
}
}
希望本文所述對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。
相關(guān)文章
js實(shí)現(xiàn)通過(guò)開(kāi)始結(jié)束控制的計(jì)時(shí)器
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)通過(guò)開(kāi)始結(jié)束控制的計(jì)時(shí)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
javascript框架設(shè)計(jì)讀書筆記之?dāng)?shù)組的擴(kuò)展與修復(fù)
本文是司徒正美的《javascript框架設(shè)計(jì)》的第三章第2節(jié)的讀書筆記,本節(jié)主要介紹的是javascript數(shù)組的擴(kuò)展與修復(fù),本文則是選取了其中的重點(diǎn)部分展示給大家。2014-12-12
前端小程序?qū)崿F(xiàn)預(yù)覽pdf并導(dǎo)出完整代碼
這篇文章主要介紹了小程序中無(wú)法直接導(dǎo)出PDF或文檔的解決方案,通過(guò)調(diào)用API下載文件并打開(kāi),文中通過(guò)代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2025-01-01
JavaScript代碼實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
這篇文章主要為大家詳細(xì)介紹了JavaScript代碼實(shí)現(xiàn)簡(jiǎn)單計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
全面了解addEventListener和on的區(qū)別
下面小編就為大家?guī)?lái)一篇全面了解addEventListener和on的區(qū)別。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07
JavaScript?12個(gè)有用的數(shù)組技巧
數(shù)組是Javascript最常見(jiàn)的概念之一,它為我們提供了處理數(shù)據(jù)的許多可能性,熟悉數(shù)組的一些常用操作是很有必要的。本文將為大家介紹12個(gè)有用的JavaScript數(shù)組技巧,需要的朋友可以參考一下2021-12-12
Layui 解決表格異步調(diào)用后臺(tái)分頁(yè)的問(wèn)題
今天小編就為大家分享一篇Layui 解決表格異步調(diào)用后臺(tái)分頁(yè)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10

