Removing empty Input Elements from a form

后端 未结 4 1110
梦如初夏
梦如初夏 2021-01-14 21:20

I have a simple form that goes on to create all the form and validation requirements for codeigniter. What I want to do is filter out any empty inputs prior to serializatio

相关标签:
4条回答
  • 2021-01-14 21:43

    To hide elements that have no value assigned:

    $('input:text[value=""]').hide();
    

    But, of course, if a value="x" attribute is provided in the html this will result in the element being shown.

    0 讨论(0)
  • 2021-01-14 21:55

    This will remove all the text fields which have a value of length 0:

    $('#send').click(function(){ 
    $(':input[type="text"]').filter(function(e){
        if (this.value.length===0){
          return true;
        }  
    }).remove();
        });
    

    example: http://jsfiddle.net/niklasvh/ZBSyX/

    0 讨论(0)
  • 2021-01-14 21:58

    You should use a regex expression using \s as the search query. So /^(\s)*$/ as the regex and just make sure input does not match this.

    Sorry but I am not familiar with Jquery or I would write the code out exactly.

    0 讨论(0)
  • 2021-01-14 22:04
        $('#send').click(function(){ 
    //---------------------------------------   
        $(":input").each(function() {
        if($(this).val() === "")
        alert("Empty Fields!!"); //using alert just to see if empty fields are detected.
        return false;
    });
    

    And you're not getting an error from this? The first lambda's scope isn't closed.

    Use Firebug to highlight errors that you might be getting and post those.

    0 讨论(0)
提交回复
热议问题