今天遇到了一個問題,用戶在在前端的input裏面輸入id的時候,多寫了個空格,數據庫裏面就找不到id了,所以無法獲取輸入的id所綁定的標籤位置在哪裏,現在需要在前端做一個處理,input框輸入的數值裏面,會自動消除空格。
代碼示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="modal-body form">
<div class="form-group">
<label class="col-sm-2 control-label" >資產編號<span class="required"> * </span></label>
<div class="col-sm-6">
<input type="text" class="form-control" id="assetId" onkeyup="this.value=this.value.replace(/[, ]/g,'')"></input>
</div>
</div>
</div>
</body>
</html>
這段代碼的作用:
onkeyup="this.value=this.value.replace(/[, ]/g,'')"
1:在input框裏面輸入編號的過程中,出現空格,自動消除
2:input首尾出現空格,自動消除
3:複製粘貼的文本里面出現空格,自動消除
原文作者:祈澈姑娘 技術博客:https://www.jianshu.com/u/05f416aefbe1
90後前端妹子,愛編程,愛運營,文藝與代碼齊飛,魅力與智慧共存的程序媛一枚。
堅持總結工作中遇到的技術問題,堅持記錄工作中所所思所見