[英]How to check if my form is valid after click on `add_button` and if is not than change color of input/select?
HTML: HTML:
<select id="id_myselect" name="myselect">
<option value="0">---------</option>
<option value="1">1</option>
<option value="7">2</option>
<option value="10">3</option>
</select>
<select id="id_book" name="book">
<option value="0">---------</option>
<option value="1">Book1</option>
</select>
<input id="id_number" name="number" type="number" />
<div id="add_button">Add</div>
How to check if my form is valid after click on add_button
and if is not than change color of input/select? 单击add_button
后如何检查我的表单是否有效,是否不更改输入/选择的颜色?
My try with jQuery: 我对jQuery的尝试:
if($( "#id_myselect option:selected" ).val() == 0){
$("#id_myselect").addClass( "error" );
}
else if($( "#id_book option:selected" ).val() == 0){
$("#id_book").addClass( "error" );
}
else if($( "id_number" ).val() <= 0){
$("#id_number").addClass( "error" );
}
Try this on clicking
of a div
like 像clicking
div
这样尝试
$(function(){
$('#add_button').on('click',function(){
if($( "#id_myselect" ).val() == 0){// remove option:selected
$("#id_myselect").addClass( "error" );
}else if($( "#id_book" ).val() == 0){// remove option:selected
$("#id_book").addClass( "error" );
}
else if($( "#id_number" ).val() <= 0){
$("#id_number").addClass( "error" );
}
});
});
onclick of add_button Check $('.error').length
. add_button的onclick检查$('.error').length
。 If it is zero then there is no error: 如果为零,则没有错误:
$('#add_button').on('click',function(){
if($('.error').length!=0){
console.log('Error exist');
}
});
And when it comes to changing color, add css to .error
class. 而当涉及到更改颜色时,请将CSS添加到.error
类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.