[英]Easy question on a jQuery script
I have the following think. 我有以下想法。
When a user types a number bigger than 10 then there is a message appeared. 当用户键入大于10的数字时,将出现一条消息。 How to remove this message let's say after 2 seconds ?
假设2秒钟后如何删除此消息?
Also if you type a number let's say 8 and hit the backspace, there is a 0 appeared. 另外,如果您输入数字,例如8,然后按退格键,则会出现0。 How to have number 1 appeared in the textbox from the beginning but also any time that the user hits backstage to delete his choice like my example above?
怎样使数字1从一开始就出现在文本框中,而且在用户按下后台删除其选择的任何时候都出现在文本框中,就像上面的示例一样? I tried to add
value="1"
into the textbox but it does not show the result in the div from the beginning. 我试图将
value="1"
添加到文本框中,但从头开始并没有在div中显示结果。
Thank you for your help 谢谢您的帮助
UPDATE I had a small problem with the fiddle, I updated it 更新我对小提琴有一个小问题,我对其进行了更新
已经有一段时间了,但是尝试一下:
$('#message').delay(2000).hide();
You should use: 你应该使用:
You can use settimeout for this: 您可以为此使用settimeout:
setTimeout(function(){
// remove the message
}, 2000); // time in miliseconds
Also your code does not work and display's NaN when I try to enter something. 另外,当我尝试输入某些内容时,您的代码无法正常工作并显示NaN。
Also use: 还可以使用:
var code = (e.keyCode ? e.keyCode : e.which);
to find out which key was pressed: 找出按下了哪个键:
jQuery Event Keypress: Which key was pressed? jQuery事件Keypress:按下了哪个键?
EDIT 编辑
If you want the value to be displayed at page load add the stuff to an function and call it on document ready: 如果要在页面加载时显示该值,则将内容添加到函数中,并在准备好文档时调用它:
http://jsfiddle.net/pBeM8/20/ http://jsfiddle.net/pBeM8/20/
EDIT2 EDIT2
Updated fiddle to also display 1 after using backspace 更新小提琴以在使用退格键后也显示1
Something like 就像是
$('#purchases').keydown(function(e){
if(!(e.which >= 48 && e.which <=58) && e.which !== 8 && e.which !== 46 && e.which !== 37 && e.which !== 39){
e.preventDefault();
}
})
.keyup(function(){
val = parseInt($(this).val() || 0, 10);
if(val > 10){
val = 10;
$(this).val(10);
$('#message').text("no more than 10");
setTimeout(function() { $('#message').text(""); }, 2000);
}
$('#amount').text(val * 10);
});
EDIT: 编辑:
In line with youer comment, here's a revised version 根据您的评论,这是修订版
var purchases = $('#purchases'),
amount = $('#amount'),
message = $('#message');
purchases.keydown(function(e) {
if (!(e.which >= 48 && e.which <= 58) && e.which !== 8 && e.which !== 46 && e.which !== 37 && e.which !== 39) {
e.preventDefault();
}
}).keyup(function() {
var val = purchases.val();
if (!val) {
amount.text(1);
return;
}
val = parseInt(val, 10);
if (val > 10) {
val = 10;
$(this).val(10);
message.text("no more than 10");
setTimeout(function() {
message.text("");
}, 2000);
}
amount.text(val * 10);
}).val(1);
This would work: 这可行:
$('#purchases').val(1).keyup()
after you add your handlers: http://jsfiddle.net/pBeM8/16/ 添加处理程序后: http : //jsfiddle.net/pBeM8/16/
It looks like you could benefit from running parseInt()
on the input that you read in. 看起来您可以通过在读取的输入上运行
parseInt()
来受益。
Alternatively, you can go for the cool HTML5 solution by adding a pattern
attribute to your input
element, assuming your input is wrapped in a form and submitted: 另外,您可以通过在您的
input
元素中添加一个pattern
属性来使用很酷的HTML5解决方案,假设您的输入被包装在表单中并提交了:
<form>
<input type="text" pattern="^\d$" />
</form>
Should allow only one numeric character as input. 应该只允许输入一个数字字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.