jQuery Spinner:非数值 [英] jQuery Spinner: Non-numerical values

查看:101
本文介绍了jQuery Spinner:非数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery Spinner,设置了min(0)和max(500)值。我该怎么做才能防止用户在输入框中直接输入非数值(或0-500范围之外的值)?当用户使用微调按钮时,最小值和最大值都有效,但在输入表单中输入内容时则不起作用。

I am using the jQuery Spinner, with min (0) and max (500) values set up. What can I do to prevent the user from directly entering non-numerical values (or values outside the 0-500 range) in the input box? The min and max values work when the user uses the spinner buttons, but not when something is typed into the input form.

推荐答案

可以使用这样的技术强制数字输入:

You can force numeric input using a technique like this:

var MIN = 0;
var MAX = 500;

$('#foo').on('keyup', function(e) {
  var v = parseInt($(this).val());
  if (isNaN(v)) {
     return $(this).val(MIN);
  }
  if ($(this).val() < MIN) {
     $(this).val(MIN);
  } else if ($(this).val() > MAX) {
     $(this).val(MAX); 
  } else {
     $(this).val(v);
  }
});

(参见示例: http://jsfiddle.net/foxbunny/ustFf/

但我不推荐 STRONG>。这不是预期的文本框行为,并且它往往会使至少一些用户感到困惑。因此,如果值不在预期范围内,最好只显示警告。

But I do not recommend it. It's not the expected text box behavior, and it tends to confuse at least some of the users. So it's better to just display a warning if the value is not in the expected range.

这篇关于jQuery Spinner:非数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆