我正在尝试编写一些验证来检查并查看字段是否大于某个长度以及是否抛出错误。我似乎无法让它工作,当我搜索某些东西时,通常采用.val()路线进行验证。
所以我有
$('.btn-danger').on('click', function(){
if ($('input[name="spine_text"]').length > 22) {
$('.spineError').html('This field must only have 22 characters.');
$('.spineError').addClass('marginBottom');
$('html, body').animate({
scrollTop: ($('.cloth').first().offset().top)
},500);
$('.clothError').html('');
return false;
}
});
我也试过.length() > 22
。他们都没有工作。
答案 0 :(得分:4)
您需要检查输入字段的值的长度,使用.val()
来获取值。
if ($('input[name="spine_text"]').val().length > 22) {
演示:Fiddle
当您说$('input[name="spine_text"]').length
时,它会返回名称为spine_text