Progress bar form fill out with jquery

2019-02-07 17:29发布

问题:

I am trying to create a progress bar that increases as a form is being filled out. For example if I had 5 inputs. Each input would need to be filled out to get to 100% My goal is to activate a button once the form reaches 100%.

My thought process was to do a bunch of if blank make it a value of 0 but if not then it gets a value of 10, or some arbitrary number.

Then I would just add up those values to complete the progress bar.

I am not sure how to set up the values so that I can add them up or this is even the right way to go. I am not sure where to go with it. Here is the code:

<form>
One<input id="1" type="name" name="name"><br />
Two<input id="2" type="name" name="name"><br />
Three<input id="3" type="name" name="else"><br />
Four<input id="4" type="name" name="name"><br />
Five<input id="5" type="name" name="name"><br />

</form>


<div id="progressbar"></div>

$(function() {
$( "#progressbar" ).progressbar({
  value: 37
});

});

$(function(){
 if ( $('#1').val() == '' ){value == 0};
 } else {
     value == 10;  };
if ( $('#2').val() == '' ){value == 0};
 } else {
     value == 10;  };
if ( $('#3').val() == '' ){value == 0};
 } else {
     value == 10;  };
if ( $('#4').val() == '' ){value == 0};
 } else {
     value == 10;  };
if ( $('#5').val() == '' ){value == 0};
 } else {
     value == 10;  };
});

Any help would be greatly appreciated

回答1:

HTML:

<form>
One<input id="1" type="name" name="name" class='moo'><br />
Two<input id="2" type="name" name="name" class='moo'><br />
Three<input id="3" type="name" name="else" class='moo'><br />
Four<input id="4" type="name" name="name" class='moo'><br />
Five<input id="5" type="name" name="name" class='moo'><br />
</form>
<div id="progressbar"></div>

Javascript:

$(function() {
$( "#progressbar" ).progressbar({
  value: 0
});

$(".moo").change(function() {
    var pbVal = 0;
    if ($("#1").val().length > 0) pbVal += 20;
    if ($("#2").val().length > 0) pbVal += 20;
    if ($("#3").val().length > 0) pbVal += 20;
    if ($("#4").val().length > 0) pbVal += 20;
    if ($("#5").val().length > 0) pbVal += 20;
    $( "#progressbar" ).progressbar( "option", "value", pbVal );
    return false;
});

});



回答2:

HTML:

<form>
One<input id="1" type="name" name="name" data-pbVal="60"><br />
Two<input id="2" type="name" name="name" data-pbVal="5"><br />
Three<input id="3" type="name" name="name" data-pbVal="5"><br />
Four<input id="4" type="name" name="name" data-pbVal="0"><br />
Five<input id="5" type="name" name="name" data-pbVal="30"><br />
</form>
<div id="progressbar"></div>

Javascript:

$(function() {
$( "#progressbar" ).progressbar({
  value: 0
});

//Register this function to fire whenever a value changes in one of the input elements
$("form input").change(function() {
    var pbVal = 0;
    //For each input element, count the value on the data-pbVal element and add it to the total
    $("form input").each(function(index) {
        if($(this).val().length > 0) {
            pbVal += $(this).data('pbVal');
        }
    });
    $( "#progressbar" ).progressbar( "option", "value", pbVal );
        return false;
    });
});