对于文本框和数字,如果它们是空的,我们很容易发送以下错误命令:
Ajax
parent_fieldset.find('input[type="text"], input[type="number"]').each(function() {
if( $(this).val() == "" ) {
$(this).addClass('input-error');
next_step = false;
}
else {
$(this).removeClass('input-error');
}
});
CSS
.input-error {
border-color: red;
}
但是对于select option
、checkbox
、radio
标签,如果是空的,怎么办?
最佳答案
你可以使用这样的东西:
$(document).ready(function(){
$('#mySelectBox').each(function() {
if($(this).val() == -1)
$(this).addClass('input-error');
else
$(this).removeClass('input-error');
});
});
fiddle 示例 here .为了澄清一点,我在这里假设您有一个值为 -1 的“请选择选项”(因此实际上没有选择任何内容):
关于javascript - 选择选项、复选框、单选标记,如果它们为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53077272/