Check if inputs are empty using jQuery

前端 未结 18 869
灰色年华
灰色年华 2020-11-22 09:14

I have a form that I would like all fields to be filled in. If a field is clicked into and then not filled out, I would like to display a red background.

Here is my

相关标签:
18条回答
  • 2020-11-22 09:42

    You can try something like this:

    $('#apply-form input[value!=""]').blur(function() {
        $(this).parents('p').addClass('warning');
    });
    

    It will apply .blur() event only to the inputs with empty values.

    0 讨论(0)
  • 2020-11-22 09:43

    With HTML 5 we can use a new feature "required" the just add it to the tag which you want to be required like:

    <input type='text' required>

    0 讨论(0)
  • 2020-11-22 09:44
    if ($('input:text').val().length == 0) {
          $(this).parents('p').addClass('warning');
    }
    
    0 讨论(0)
  • 2020-11-22 09:45

    Here is an example using keyup for the selected input. It uses a trim as well to make sure that a sequence of just white space characters doesn't trigger a truthy response. This is an example that can be used to begin a search box or something related to that type of functionality.

    YourObjNameSpace.yourJqueryInputElement.keyup(function (e){
       if($.trim($(this).val())){
           // trimmed value is truthy meaning real characters are entered
        }else{
           // trimmed value is falsey meaning empty input excluding just whitespace characters
        }
    }
    
    0 讨论(0)
  • 2020-11-22 09:46

    A clean CSS-only solution this would be:

    input[type="radio"]:read-only {
            pointer-events: none;
    }
    
    0 讨论(0)
  • 2020-11-22 09:48

    you can use also..

    $('#apply-form input').blur(function()
    {
        if( $(this).val() == '' ) {
              $(this).parents('p').addClass('warning');
        }
    });
    

    if you have doubt about spaces,then try..

    $('#apply-form input').blur(function()
    {
        if( $(this).val().trim() == '' ) {
              $(this).parents('p').addClass('warning');
        }
    });
    
    0 讨论(0)
提交回复
热议问题