如何关注值为空的所有类字段?

时间:2017-01-24 11:56:23

标签: javascript jquery validation array.prototype.map

<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;
}

我想只关注那些空的......我该如何解决这个问题呢?

5 个答案:

答案 0 :(得分:1)

使用.filter()获取其值为空的所有文本框,而不是其他内容请查看以下代码段

&#13;
&#13;
$(".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;
&#13;
&#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选择器。

&#13;
&#13;
$(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;
&#13;
&#13;

答案 4 :(得分:0)

选择器可能更简单。

&#13;
&#13;
// 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;
&#13;
&#13;