<input type ="text" class="searchskill" value="">
<input type ="text" class="searchskill" value="3">
<input type ="text" class="searchskill" value="">
<input type ="text" class="searchskill" value="4">
如何关注值为空的所有类字段?
var skillname =$(".searchskill").map(function() {
if($(this).val()==''){
return false;
}
}).get();
if(jQuery.isEmptyObject(skillname)){
} else {
alert("You Can Not Keep Skill Name As Blank");
$(".searchskill").css({"border-style": "solid", "border-color": "red"});
$(".searchskill").focus();
return false;
}
我想只关注那些空的......我该如何解决这个问题呢?
答案 0 :(得分:1)
使用.filter()获取其值为空的所有文本框,而不是其他内容请查看以下代码段
$(".clssubmit").on("click", function() {
var filteredList = $('.searchskill').filter(function() {
return $(this).val() == "";
});
if (filteredList.length > 0) {
filteredList.css("border", "1px solid red");
return false;
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="searchskill" value="">
<input type="text" class="searchskill" value="3">
<input type="text" class="searchskill" value="">
<input type="text" class="searchskill" value="4">
<input type="button" value="submit" class="clssubmit" />
&#13;
答案 1 :(得分:1)
$(".searchskill").each(function() {
if($(this).val() === "") {
$(this).css({"border-style": "solid", "border-color": "red"});
}
});
答案 2 :(得分:1)
假设您正在使用jQuery;
{{1}}
答案 3 :(得分:1)
据我了解,focus
你的意思是highlight
所有元素。如果是这样,您可以尝试attribute
选择器。
$(function(){
$('.searchskill[value=""]').addClass('error')
})
&#13;
.error{
border: 1px solid red;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<input type ="text" class="searchskill" value=" ">
<input type ="text" class="searchskill" value="3">
<input type ="text" class="searchskill" value="">
<input type ="text" class="searchskill" value="4">
<input type ="text" class="searchskill" value="">
<input type ="text" class="searchskill" value="4">
<input type ="text" class="searchskill" value="">
<input type ="text" class="searchskill" value="4">
<input type ="text" class="searchskill" value="">
<input type ="text" class="searchskill" value="4">
<input type ="text" class="searchskill" value="">
<input type ="text" class="searchskill" value="4">
&#13;
答案 4 :(得分:0)
选择器可能更简单。
// highlight all empty fields and focus on the first one
$('.searchskill[value=""]').css({"border-style": "solid", "border-color": "red"})[0].focus();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type ="text" class="searchskill" value="">
<input type ="text" class="searchskill" value="3">
<input type ="text" class="searchskill" value="">
<input type ="text" class="searchskill" value="4">
&#13;