我有这个问题。我的用户可以点击-
按钮从总数中减去1。它们不能低于0
。当我点击我的按钮0
而不是点击+
按钮以上0
时,它就可以了。但是,当我想再次点击-
按钮时,它仍然被禁用。
这是我的代码
$('#minderApple').click(function(){
$("#appletotal").val( parseInt($("#appletotal").val()) - 1);
if ($('#appletotal').val() < 1){
$("#minderApple").prop("disabled",true);
}else{
$("#minderApple").prop("disabled",false);
}
});
答案 0 :(得分:0)
问题是因为val()
返回一个字符串,您将其与整数进行比较。您可以使用parseInt()
将值转换为int。此外,您可以通过直接向if
提供布尔值来删除prop()
语句。试试这个:
$('#minderApple').click(function() {
var $appleTotal = $('#appletotal');
var newVal = parseInt($appleTotal.val(), 10) - 1;
$appleTotal.val(newVal);
$(this).prop("disabled", newVal < 1);
});
答案 1 :(得分:0)
$('#minderApple').click(function(){
if ((parseInt($("#appletotal").val()) - 1) < 1){
$("#minderApple").attr("disabled","disabled");
}else{
$("#minderApple").removeAttr("disabled");
}
});
答案 2 :(得分:0)
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<style>
.active {
color:red;
}
</style>
<script>
$(document).ready(function () {
$('#minderApple').click(function () {
$("#appletotal").val(parseInt($("#appletotal").val()) - 1);
if ($('#appletotal').val() < 1) {
$("#minderApple").prop("disabled", true);
} else {
$("#minderApple").prop("disabled", false);
}
});
$('#plus').click(function () {
$("#appletotal").val(parseInt($("#appletotal").val()) + 1);
if ($('#appletotal').val() >1) {
$("#minderApple").prop("disabled", false);
} else {
$("#minderApple").prop("disabled", false);
}
});
});
</script>
</head>
<body>
<input type="text" id="appletotal" value="5"/>
<input type="button" id="minderApple" value="-"/>
<input type="button" id="plus" value="+"/>
</body>
</html>