Filter Form Parameters Before Submitting

后端 未结 5 1058
野的像风
野的像风 2020-12-20 07:18

Is there a way to check the parameter if its empty or not before submitting?

5条回答
  •  悲&欢浪女
    2020-12-20 07:54

    Using pure javascript, it would be something like - (untested)

    var form = document.forms[0];
    form.onsubmit=function(){
        var inputs, index;
        inputs = document.getElementsByTagName('input');
        for (index = 0; index < inputs.length; ++index) {
             if(inputs[index].value == ''){
                form.removeChild(inputs[index]);
             }
        }
    }
    

    Using jQuery, it would be something like - (untested)

    $('form').submit(function() {
         $('input').each(function(){
            if($(this).val() == ''){
               $(this).remove();
            }
         });
    });
    

提交回复
热议问题