我有一个表格,我希望所有的字段都被填写。如果单击一个字段,然后没有填写,我想显示一个红色背景。

这是我的代码:

$('#apply-form input').blur(function () {
  if ($('input:text').is(":empty")) {
    $(this).parents('p').addClass('warning');
  }
});

无论字段是否填写,它都应用警告类。

我做错了什么?


当前回答

一个干净的css解决方案是:

input[type="radio"]:read-only {
        pointer-events: none;
}

其他回答

<script type="text/javascript">
$('input:text, input:password, textarea').blur(function()
    {
          var check = $(this).val();
          if(check == '')
          {
                $(this).parent().addClass('ym-error');
          }
          else
          {
                $(this).parent().removeClass('ym-error');  
          }
    });
 </script>// :)

在HTML 5中,我们可以使用一个新功能“required”,只需将它添加到你想要被要求的标签中:

<input type='text' required>

请使用此代码输入文本

$('#search').on("input",function (e) {

});

if ($('input:text').val().length == 0) {
      $(this).parents('p').addClass('warning');
}
if($("#textField").val()!=null)

这对我很有用