我正在使用JavaScript从隐藏字段中提取值并将其显示在文本框中.隐藏字段中的值已编码.
例如,
<input id='hiddenId' type='hidden' value='chalk & cheese' />
Run Code Online (Sandbox Code Playgroud)
被拉进去
<input type='text' value='chalk & cheese' />
Run Code Online (Sandbox Code Playgroud)
通过一些jQuery从隐藏字段中获取值(此时我丢失了编码):
$('#hiddenId').attr('value')
Run Code Online (Sandbox Code Playgroud)
问题是,当我chalk & cheese从隐藏字段中读取时,JavaScript似乎失去了编码.为了逃避chalk & cheese和amp;,我想编码保留.
是否有一个JavaScript库或jQuery方法将对字符串进行HTML编码?
如果我写的话,我想有一些功能
<textarea maxlength="50"></textarea>
<textarea maxlength="150"></textarea>
<textarea maxlength="250"></textarea>
Run Code Online (Sandbox Code Playgroud)
它会自动在textArea上施加maxlength.如果可能请不要在jQuery中提供解决方案.
注意:如果我这样做,可以这样做:
<textarea onkeypress="return imposeMaxLength(event, this, 110);" rows="4" cols="50">
function imposeMaxLength(Event, Object, MaxLen)
{
return (Object.value.length <= MaxLen)||(Event.keyCode == 8 ||Event.keyCode==46||(Event.keyCode>=35&&Event.keyCode<=40))
}
Run Code Online (Sandbox Code Playgroud)
复制在HTML textarea上模拟HTML输入"maxlength"属性的最佳方法是什么?
但问题是我每次声明textArea时都不想写onKeyPress和onKeyUp.
我正在为这个网站上的textarea开发一个字符数.现在,它说NaN,因为它似乎找不到字段中有多少字符的长度,在开头是0,所以数字应该是500.在Chrome开发人员工具的控制台中,不会发生错误.我的所有代码都在网站上,我甚至尝试使用jQuery常规JavaScript来处理textarea字段的字符数,但似乎没有任何效果.
请告诉我在我的contact.js文件中的jQuery和JavaScript代码中我做错了什么.
$(document).ready(function() {
var tel1 = document.forms["form"].elements.tel1;
var tel2 = document.forms["form"].elements.tel2;
var textarea = document.forms["form"].elements.textarea;
var clock = document.getElementById("clock");
var count = document.getElementById("count");
tel1.addEventListener("keyup", function (e){
checkTel(tel1.value, tel2);
});
tel2.addEventListener("keyup", function (e){
checkTel(tel2.value, tel3);
});
/*$("#textarea").keyup(function(){
var length = textarea.length;
console.log(length);
var charactersLeft = 500 - length;
console.log(charactersLeft);
count.innerHTML = "Characters left: " + charactersLeft;
console.log("Characters left: " + charactersLeft);
});​*/
textarea.addEventListener("keypress", textareaLengthCheck(textarea), false);
});
function checkTel(input, nextField) {
if (input.length == 3) {
nextField.focus();
} else …Run Code Online (Sandbox Code Playgroud) I'm stuck with a problem using jQuery Validate. The thing is I have a few textboxes with their maxlength attributes set. Although they are not required, they still fail to validate when either empty or whitespace.
Sample HTML:
<input type="textbox" id="tbx1" maxlength="100" value="" />
Then, $('#tbx1').valid() returns false. Shouldn't it return true instead? Are there any known workarounds for this?
Oh, and adding $('#myform').validate({ ignore: '#tbx1' }); doesn't quite work for me, because I'm using ASP.NET controls with their …
问题是Firefox将换行符设为"1"(\n)字符,而Chrome将其计为"2"(\ r \n)这是我在textarea使用的内容maxlength=10:
这是Firefox的10个字符
1234
5
6
7
Run Code Online (Sandbox Code Playgroud)
这是Chrome的10个字符
1234
5
6
Run Code Online (Sandbox Code Playgroud)
尝试验证表单时出现问题.如果我有例如以下文字
012345
678
Run Code Online (Sandbox Code Playgroud)
在Firefox中,验证通过并保存数据,但是当我尝试在Chrome中执行相同操作时,它会显示警告,并阻止其发送表单. "请将此文本缩短为10个字符或更少(您目前使用11个字符)".

我认为此验证消息在Chrome中是新的,或者至少我以前没有看到它们
这是一个JSFiddle示例.要在Chrome中重现它,您应该编写一个包含10个字符的字符串,然后删除1并按Enter键添加新行.
以下是JavaScript如何使用js计算JSFiddle长度计数的示例.
不知道哪一个是新行(1或2)的正确值.但是由于数据库(在我的情况下是Postgres)和JavaScript认为它是"1"字符,我想知道是否有办法让Chrome做同样的事情.
我无法弄清楚为什么我的 JavaScript 无法正常工作。它与j Query一起使用,它应该选择所有文本区域标签,然后为每个文本区域计算在其中键入的字符数,然后在达到最大长度后不允许用户再输入,还显示字符计数器在每个文本区域框下。它所做的只是显示剩余的字符,但在按下每个键后不会递减,并且在达到最大长度时也不会停止。它也不会选择所有文本区域,它只选择找到的第一个文本区域。
这是我的 TextAreaCounter.js
$(document).ready(function){
var $texts = $('textarea[maxlength]');
$texts.each(function){
$var $this = $(this),
max = $(this).attr('maxlength'),
textId = $(this)attr.('id'),
$parent = $this.parent(),
countId = textId+'-count';
$div = $('<div>Characters Remaining: </div>').addClass('count-down').insertAfter($this);
$input = $('<input></input>').attr({
type:"text",
readOnly: "readOnly",
value: max,
id: countId
}).css({
width: "25px"
marginTop: "5px"
marginBottom: "10px"
}).addClass('readOnly').appendTo($div);
$this.on({
keyup: function(){
val = $this.val(),
countVal = $('#'+countId).val(),
if(val.length > max){
$this.val(val.substr(0,max));
alert('Max length exceeded: ' +max);
return false;
}else{
$('#'+countId).val(max-val.length);
}
},
blur: function(){
val=$this.val();
if(val.length > …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个模拟手机短信的角色计数器.我有计算字符,空格和换行符,但我的计数器不同.对于换行符,它在message.length的计数中递减1,但在maxlength中递减多次.我想在缩进时添加更多空格,但不确定如何使message.length集合相同maxlength.
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app>
<textarea ng-model="message" ng-trim="false" maxlength="160"></textarea>
<span>{{160 - message.length}} left</span>
</body>Run Code Online (Sandbox Code Playgroud)
此答案实际上并不适用于此(它将空格和换行符计为1): angularjs文本区域字符计数器
使用以下代码,将删除超出指定最大值的任何输入.但这会产生一种效果,即键入一个字符然后立即删除.我宁愿简单地阻止输入字符.
<textarea id="textarea" onKeyDown="limitText()" onKeyUp="limitText()">
</textarea>
<span name="charcount_text" id="charcount_text"></span>
<script type="text/javascript">
function limitText() {
var count = document.getElementById('textarea').value.length;
var remaining = 4000 - count;
if(remaining <= 0) {
document.getElementById('charcount_text').innerHTML = '4000 character limit reached.' ;
document.getElementById('textarea').value = document.getElementById('textarea').value.substring(0, 4000);
} else if(remaining <= 50) {
document.getElementById('charcount_text').innerHTML = '4000 character limit, ' + remaining + ' remaining.';
} else {
document.getElementById('charcount_text').innerHTML = '';
}
}
</script>
Run Code Online (Sandbox Code Playgroud)