我知道这个问题已经问过几次了,但似乎没有一个答案可行,或者至少我已经找到了。
在检查来自表单输入的输入后,我需要从列表项中选择错误类别。
这是我所拥有的:
的HTML:
<ul>
<li class="label"><label>Label</label></li>
<li class="input"><input name="fname" type="text" /></li>
<li class="icon"></li>
<li class="clearfix"></li>
<li class="error"></li>
<li class="label"><label>Label</label></li>
<li class="input"><input name="lname" type="text" /></li>
<li class="icon"></li>
<li class="clearfix"></li>
<li class="error"></li>
<li class="label"><label>Label</label></li>
<li class="input"><input name="uname" type="text" /></li>
<li class="icon"></li>
<li class="clearfix"></li>
<li class="error"></li>
<li class="label"><label>Label</label></li>
<li class="input"><input name="password" type="password" /></li>
<li class="icon"></li>
<li class="clearfix"></li>
<li class="error"></li>
</ul>
jQuery:
$('input[name=uname]').on("input", function() {
var input = $.trim(this.value);
var BLIDRegExpression = /^[a-zA-Z0-9]+$/;
if(input == 0){
uname_status = false;
$(this).closest('li').next('.icon').html('<img src="/assets/images/png/glyphicons_199_ban.png" />');
$(this).parent('li').next().next().next('li').show();
$(this).parent('li').next().next().next('li').html('<span>You must enter a Username!</span>');
return false;
}
});
上面的方法确实有效,我只是想知道是否有比使用以下方法更好的选择“错误”类的方法:
$(this).parent('li').next().next().next('li').show();
我尝试了以下方法,但它们没有起作用:
// From another stackoverflow question
$(this).closest('li').nextAll(':has(.error):first').find('.error');
$(this).parents('li').eq(0).next().find('li.error');
// Works but selects the next error class' and the one after
$(this).closest('li').nextAll('li.error').show();
or
$(this).parent('li').nextAll('li.error').show();
还有其他解决方案吗?我只是想念一些简单的东西吗?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句