[英]Check if input has specific value
I am using Jquery to check if an input has a specific value and if it does have that value it will enable the submit button.我正在使用 Jquery 检查输入是否具有特定值,如果它确实具有该值,它将启用提交按钮。 The problem is I set the value to 4 but if 44 (or anything that begins with 4) is entered it still enables the button.
问题是我将值设置为 4,但如果输入 44(或以 4 开头的任何内容),它仍会启用按钮。 Also once 4 is entered it can be changed to anything and the button remains enabled.
此外,一旦输入 4,它可以更改为任何内容,并且按钮保持启用状态。
What I would like it to do is only change to enabled if the value is 4 and if the value is changed then the the submit button should be disabled.我希望它做的是仅在值为 4 时更改为启用,如果值更改,则应禁用提交按钮。
Jquery查询
$(document).ready(function() {
$('#check').keyup(function() {
if($(this).val() === '4') {
$('.submit').removeAttr('disabled');
}
});
});
HTML HTML
<input id="check" type="text" name="check" />
<input type="submit" name="submit" class="submit" disabled="disabled">
Try this:试试这个:
$('#check').change(function() {
if($(this).val() === '4') {
$('.submit').removeAttr('disabled');
}
else $('.submit').attr('disabled', 'disabled');
});
in fact you need to re-disable the submit button if you wish so when the value is not 4
.实际上,当值不是
4
时,如果您愿意,您需要重新禁用提交按钮。
Better yet, instead of更好的是,而不是
$('.submit').attr('disabled', 'disabled');
you could/should use你可以/应该使用
$('.submit').prop('disabled', true);
so the handler becomes所以处理程序变成
$('#check').change(function() {
if($(this).val() === '4') {
$('.submit').removeAttr('disabled');
}
else $('.submit').prop('disabled', true);
});
or even simpler甚至更简单
$('#check').change(function() {
$('.submit').prop('disabled', $(this).val() !== '4');
});
Its happening because you are not disabling button if value is not 4.它的发生是因为如果值不是 4,你没有禁用按钮。
$('#check').keyup(function() {
if($(this).val() === '4') {
$('.submit').removeAttr('disabled');
}
else{
$('.submit').attr('disabled','disabled');
}
});
Simply add an else that disables it :)只需添加一个禁用它的 else :)
$(document).ready(function() {
$('#check').keyup(function() {
if($(this).val() === '4') {
$('.submit').removeAttr('disabled');
} else {
$('.submit').prop('disabled', true);
}
});
});
You need to redisable it.您需要重新禁用它。
$(document).ready(function() {
$('#check').change(function() {
if($(this).val() === '4') {
$('.submit').removeAttr('disabled');
}else{
$('.submit').prop('disabled');
}
});
Use:使用:
$('#check').keyup(function () {
$('.submit').prop('disabled', $(this).val() !== '4' );
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.