首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >达到最大输入长度时的工具提示

问达到最大输入长度时的工具提示
EN

Stack Overflow用户
提问于 2010-11-16 14:03:48
回答 2查看 3.3K关注 0票数 1

是否可以为输入类型的文本提供工具提示,以防该文本框在html中达到最大长度?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-16 14:43:57

可以,您可以将工具提示设置为文本框

代码语言:javascript
复制
/*
 txtname id of youe textbox
 val is value of textbox
 ccount is max length of textbox. 
 if ccount is set to 4 then 
 if value of textbox exceed then 4 then it will show tooltip
 */
function txtToolTip(txtname, val, ccount) {
    var txtTool = document.getElementById(txtname);
    if (txtTool.value.length > ccount) {
        //Setting tool tip value.
        txtTool.title = txtTool.value;
    }
}

您的输入框是

代码语言:javascript
复制
<input type="text" name="txtnm" id="txtnm" onkeyup="txtToolTip(this.id, this.value, 4);"/>
票数 1
EN

Stack Overflow用户

发布于 2016-07-08 00:55:58

你可以使用我的代码来解决你的问题:

​

代码语言:javascript
复制
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Input Key Filter Test</title>
	<meta name="author" content="Andrej Hristoliubov anhr@mail.ru">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	
	<!-- For compatibility of IE browser with audio element in the beep() function.
	https://www.modern.ie/en-us/performance/how-to-use-x-ua-compatible -->
	<meta http-equiv="X-UA-Compatible" content="IE=9"/>
	
	<link rel="stylesheet" href="https://rawgit.com/anhr/InputKeyFilter/master/InputKeyFilter.css" type="text/css">		
	<script type="text/javascript" src="https://rawgit.com/anhr/InputKeyFilter/master/Common.js"></script>
	<script type="text/javascript" src="https://rawgit.com/anhr/InputKeyFilter/master/InputKeyFilter.js"></script>
	
</head>
<body>
	<h1>Text field. String length is limited to 10 sumbols</h1>
    Max Length input field: 
    <input type='text' id='maxLength' maxlength ="11" />
    New string: <span id="newString"></span>
    <script>
        CreateMaxLengthFilter('maxLength', {
            formatMessage: 'Length limit to %s sumbols'
            , onerror: function (elementInput) {
                elementInput.focus();//except Firefox
            }
            , onblur: function (event) {
                document.getElementById('newString').innerHTML = event.target.value;
            }
        });
    </script>

</body>
</html>

​

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4191590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档