javascript - adding textInput values to another text input using js function in Yii 2 -
simply i'm trying add value of text input fields : http://jsfiddle.net/mak2k/1/
my form code :
<?php $script = <<< js $('#amount_value_id').keyup(function(){ var amountsum=0; $('#amount_value_id').each(function(){ if (this.value != "") amountsum+=parseint(this.value); }); // alert('foo'); $("#totalamount").text(amountsum); //console.log(amountsum); }); js; $this->registerjs($script); ?> <?php $form = activeform::begin(["id" => "project-form"]); ?> <?php foreach ($project $i => $project) { ?> <div class="col-lg-2 col-md-3" id="amount_value_id"> <?= $form->field($project, "[{$i}]amount")->textinput(); ?> </div> <?php } ?> <div class="col-lg-2" id="totalamount"> <?php echo $form->field($model, 'total_amount')->textinput(['maxlength' => 128, 'id' => 'totalamount']) ?> </div>
in text input total amount should holds total sum giving nan !! .what i'm doing ??
i tested jsfiddle. when enter numeric value right, when enter text value, gives "nan". it's obvious, because using parseint()
. when enter text "test" in input field, code tries convert "text" string integer value , result nan. workaround problem preventing user entering non-numeric value. solution using javascript isnan()
function. isnan()
checks illegal
numbers value.
Comments
Post a Comment