[英]jquery keyup function: enabling/disabling a textbox using keyup()
I am trying to disable the textbox using keyup functionality. 我试图使用键盘功能禁用文本框。 I have a TextArea and a Text Box .
我有一个TextArea和一个文本框 。 Now i use a keyup operation on backspace key, like if the length of content inside textarea is 3 it should disable the textbox.
现在我在退格键上使用键盘操作,就像textarea中的内容长度是3一样,它应该禁用文本框。 I also have an alert message which pops when the length of content in text area is 3. Code worked for the pop up but it doesnot worked for the textbox.
我还有一条警报消息,当文本区域中的内容长度为3时弹出。但代码适用于弹出窗口,但它对文本框不起作用。 What am i missing?
我错过了什么? Please help.
请帮忙。 Here is my code:
这是我的代码:
$('#comment').keyup(function() {
if (event.which == 8) {
var txt = $('#comment').val().length;
if(txt == 3)
{
alert("backspace");
$("#text1").attr("diasbled", "diasbled");
}
}
});
You need to do: 你需要这样做:
1) this.value.length
to get the total characters length of your textarea 1)
this.value.length
获取textarea的总字符长度
2) From jQuery version 1.6
, use .prop() instead of .attr()
to set the properties of an element 2)从jQuery
1.6
版开始,使用.prop()代替.attr()
来设置元素的属性
3) Correct the typo: it should be disabled
not diasbled
3)纠正错字:应该
disabled
它而不是diasbled
$('#comment').keyup(function () {
if (this.value.length >= 3) {
$("#text1").prop("disabled", true);
} else {
$("#text1").prop("disabled", false);
}
});
Use prop
instead of attr
also pass event to function 使用
prop
而不是attr
也将事件传递给函数
$('#comment').keyup(function (event) { //and event here
if (event.which == 8) {
if ($(this).val().length >= 3) {
$("#text1").prop("disabled", true);
}
}
});
Your code is fine.. but you mispelled the "disabled" in your code. 你的代码很好......但是你错误地将代码中的“禁用”。 Here's the sample..
这是样品..
<html>
<head>
<title>js test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
<input type="text" value="" id="comment" />
<input type="text" value="" id="text1" />
<script type="text/javascript">
$(document).ready(function()
{
$('#comment').keyup(function(e) {
if (e.which == 8) {
var txt = $('#comment').val().length;
if(txt == 3)
{
alert("backspace");
$("#text1").attr("disabled", "disabled");
}
}
});
});
</script>
</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.