<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>JS判断输入框的值是否为空</title> </head> <body> <input type="text" id="txt"/> <input type="button" value="检测" id="btn"> <script> var oTxt = document.getElementById(‘txt‘); var oBtn = document.getElementById(‘btn‘); function isnull(val) { var str = val.replace(/(^\s*)|(\s*$)/g, ‘‘);//去除空格; if (str == ‘‘ || str == undefined || str == null) { //return true; console.log(‘空‘) } else { //return false; console.log(‘非空‘); } } oBtn.onclick = function () { isnull( oTxt.value ); } </script> </body> </html>
时间: 2024-10-05 18:33:10