Plus/Minus 最大值输入
Plus/Minus Max Value Input
我有一个 plus/minus 按钮,我想要它,这样用户就不能 select 超过 20 岁但不知道如何使用它。我尝试使用 min="1" max="5 属性,但它们没有用。这是我的代码和 link 到 fiddle。https://jsfiddle.net/6n9298gp/
<form id='myform' method='POST' action='#' class="numbo">
<input type='button' value='-' class='qtyminus' field='quantity' style="font-weight: bold;" />
<input type='text' name='quantity' value='1' class='qty' style="margin-bottom: 0px !important" onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
<input type='button' value='+' class='qtyplus' field='quantity' style="font-weight: bold;" />
</form>
<script type="text/javascript">
jQuery(document).ready(function(){
// This button will increment the value
$('.qtyplus').click(function(e){
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name='+fieldName+']').val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
$('input[name='+fieldName+']').val(currentVal + 1);
$('.qtyminus').val("-").removeAttr('style')
} else {
// Otherwise put a 0 there
$('input[name='+fieldName+']').val(1);
}
});
// This button will decrement the value till 0
$(".qtyminus").click(function(e) {
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name='+fieldName+']').val());
// If it isn't undefined or its greater than 0
if (!isNaN(currentVal) && currentVal > 1) {
// Decrement one
$('input[name='+fieldName+']').val(currentVal - 1);
} else {
// Otherwise put a 0 there
$('input[name='+fieldName+']').val(1);
$('.qtyminus').val("-").css('color','#aaa');
$('.qtyminus').val("-").css('cursor','not-allowed');
}
});
});
</script>
我在这里更新了 jsfiddle:https://jsfiddle.net/6n9298gp/5/
基本上只是添加了一个块,该块将检查当前值是否低于 20 以允许递增,否则显示您的 "not allowed" 图标:
if (currentVal < 20)
{
$('input[name='+fieldName+']').val(currentVal + 1);
$('.qtyminus').val("-").removeAttr('style');
}
else
{
$('.qtyplus').val("+").css('color','#aaa');
$('.qtyplus').val("+").css('cursor','not-allowed');
}
还添加了一行以删除递减后不允许的光标:
// Decrement one only if value is > 1
$('input[name='+fieldName+']').val(currentVal - 1);
$('.qtyplus').val("+").removeAttr('style');
你做对了。
$('.qtyplus').click(function(e){
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name='+fieldName+']').val());
// If is not undefined
if (!isNaN(currentVal) && currentVal < 20) {
// Increment
$('input[name='+fieldName+']').val(currentVal + 1);
} else {
// Otherwise put a 0 there
//$('input[name='+fieldName+']').val(1);
$(this).val("+").css('color','#aaa');
$(this).val("+").css('cursor','not-allowed');
}
});
jQuery(document).ready(function(){
// This button will increment the value
$('.qtyplus').click(function(e){
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name='+fieldName+']').val());
// If is not undefined
if (!isNaN(currentVal) && currentVal < 20) {
// Increment
$('input[name='+fieldName+']').val(currentVal + 1);
} else {
// Otherwise put a 0 there
//$('input[name='+fieldName+']').val(1);
$(this).val("+").css('color','#aaa');
$(this).val("+").css('cursor','not-allowed');
}
});
// This button will decrement the value till 0
$(".qtyminus").click(function(e) {
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name='+fieldName+']').val());
// If it isn't undefined or its greater than 0
if (!isNaN(currentVal) && currentVal > 1) {
// Decrement one
$('input[name='+fieldName+']').val(currentVal - 1);
} else {
// Otherwise put a 0 there
$('input[name='+fieldName+']').val(1);
$(this).val("-").css('color','#aaa');
$(this).val("-").css('cursor','not-allowed');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id='myform' method='POST' action='#' class="numbo">
<input type='button' value='-' class='qtyminus' field='quantity' style="font-weight: bold;" />
<input type='text' name='quantity' value='1' class='qty' style="margin-bottom: 0px !important" onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
<input type='button' value='+' class='qtyplus' field='quantity' style="font-weight: bold;" />
</form>
尝试添加一个条件来检查小于最大值的值。
$('.qtyplus').click(function(e){
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name='+fieldName+']').val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
if(currentVal<20)
{
$('input[name='+fieldName+']').val(currentVal + 1);
$('.qtyminus').val("-").removeAttr('style');
}
} else {
// Otherwise put a 0 there
$('input[name='+fieldName+']').val(1);
}
});
<input type="number" min="1" max="20" step="1">
然后你可以使用脚本来传递验证消息(只是因为浏览器内置的数字字段消息目前很差)。
这消除了对库的依赖,遵循 HTML 规范,并且免费内置了辅助功能。
如果您仍然需要 +/- 按钮来满足设计限制,请确保使用减号 (−
),然后在正确的字段类型之上使用脚本逐步增强。这样,任何 jQuery 无法下载(例如网络故障)或页面上存在脚本错误的情况都不会导致整个程序崩溃。
如果 +/- 按钮不能满足设计要求,请考虑完全取消它们。
您还可以通过在元素中放入模式匹配来逐步增强可能无法使用 type="number"
的浏览器(虽然您可以看到 support is pretty good while pattern
support 甚至更好),但这是一个非常极端的情况:
<input type="number" min="1" max="20" step="1" pattern="[0-9]*">
您可以阅读more on type="number"
in the HTML5 spec or the language reference。
我有一个 plus/minus 按钮,我想要它,这样用户就不能 select 超过 20 岁但不知道如何使用它。我尝试使用 min="1" max="5 属性,但它们没有用。这是我的代码和 link 到 fiddle。https://jsfiddle.net/6n9298gp/
<form id='myform' method='POST' action='#' class="numbo">
<input type='button' value='-' class='qtyminus' field='quantity' style="font-weight: bold;" />
<input type='text' name='quantity' value='1' class='qty' style="margin-bottom: 0px !important" onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
<input type='button' value='+' class='qtyplus' field='quantity' style="font-weight: bold;" />
</form>
<script type="text/javascript">
jQuery(document).ready(function(){
// This button will increment the value
$('.qtyplus').click(function(e){
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name='+fieldName+']').val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
$('input[name='+fieldName+']').val(currentVal + 1);
$('.qtyminus').val("-").removeAttr('style')
} else {
// Otherwise put a 0 there
$('input[name='+fieldName+']').val(1);
}
});
// This button will decrement the value till 0
$(".qtyminus").click(function(e) {
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name='+fieldName+']').val());
// If it isn't undefined or its greater than 0
if (!isNaN(currentVal) && currentVal > 1) {
// Decrement one
$('input[name='+fieldName+']').val(currentVal - 1);
} else {
// Otherwise put a 0 there
$('input[name='+fieldName+']').val(1);
$('.qtyminus').val("-").css('color','#aaa');
$('.qtyminus').val("-").css('cursor','not-allowed');
}
});
});
</script>
我在这里更新了 jsfiddle:https://jsfiddle.net/6n9298gp/5/
基本上只是添加了一个块,该块将检查当前值是否低于 20 以允许递增,否则显示您的 "not allowed" 图标:
if (currentVal < 20)
{
$('input[name='+fieldName+']').val(currentVal + 1);
$('.qtyminus').val("-").removeAttr('style');
}
else
{
$('.qtyplus').val("+").css('color','#aaa');
$('.qtyplus').val("+").css('cursor','not-allowed');
}
还添加了一行以删除递减后不允许的光标:
// Decrement one only if value is > 1
$('input[name='+fieldName+']').val(currentVal - 1);
$('.qtyplus').val("+").removeAttr('style');
你做对了。
$('.qtyplus').click(function(e){
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name='+fieldName+']').val());
// If is not undefined
if (!isNaN(currentVal) && currentVal < 20) {
// Increment
$('input[name='+fieldName+']').val(currentVal + 1);
} else {
// Otherwise put a 0 there
//$('input[name='+fieldName+']').val(1);
$(this).val("+").css('color','#aaa');
$(this).val("+").css('cursor','not-allowed');
}
});
jQuery(document).ready(function(){
// This button will increment the value
$('.qtyplus').click(function(e){
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name='+fieldName+']').val());
// If is not undefined
if (!isNaN(currentVal) && currentVal < 20) {
// Increment
$('input[name='+fieldName+']').val(currentVal + 1);
} else {
// Otherwise put a 0 there
//$('input[name='+fieldName+']').val(1);
$(this).val("+").css('color','#aaa');
$(this).val("+").css('cursor','not-allowed');
}
});
// This button will decrement the value till 0
$(".qtyminus").click(function(e) {
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name='+fieldName+']').val());
// If it isn't undefined or its greater than 0
if (!isNaN(currentVal) && currentVal > 1) {
// Decrement one
$('input[name='+fieldName+']').val(currentVal - 1);
} else {
// Otherwise put a 0 there
$('input[name='+fieldName+']').val(1);
$(this).val("-").css('color','#aaa');
$(this).val("-").css('cursor','not-allowed');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id='myform' method='POST' action='#' class="numbo">
<input type='button' value='-' class='qtyminus' field='quantity' style="font-weight: bold;" />
<input type='text' name='quantity' value='1' class='qty' style="margin-bottom: 0px !important" onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
<input type='button' value='+' class='qtyplus' field='quantity' style="font-weight: bold;" />
</form>
尝试添加一个条件来检查小于最大值的值。
$('.qtyplus').click(function(e){
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name='+fieldName+']').val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
if(currentVal<20)
{
$('input[name='+fieldName+']').val(currentVal + 1);
$('.qtyminus').val("-").removeAttr('style');
}
} else {
// Otherwise put a 0 there
$('input[name='+fieldName+']').val(1);
}
});
<input type="number" min="1" max="20" step="1">
然后你可以使用脚本来传递验证消息(只是因为浏览器内置的数字字段消息目前很差)。
这消除了对库的依赖,遵循 HTML 规范,并且免费内置了辅助功能。
如果您仍然需要 +/- 按钮来满足设计限制,请确保使用减号 (−
),然后在正确的字段类型之上使用脚本逐步增强。这样,任何 jQuery 无法下载(例如网络故障)或页面上存在脚本错误的情况都不会导致整个程序崩溃。
如果 +/- 按钮不能满足设计要求,请考虑完全取消它们。
您还可以通过在元素中放入模式匹配来逐步增强可能无法使用 type="number"
的浏览器(虽然您可以看到 support is pretty good while pattern
support 甚至更好),但这是一个非常极端的情况:
<input type="number" min="1" max="20" step="1" pattern="[0-9]*">
您可以阅读more on type="number"
in the HTML5 spec or the language reference。