admin管理员组

文章数量:1271780

Simple Question I just cant solve, I have the following function:

function finalCheck(){

        var result = true;
        jQuery("#carloan-form :input[type=text]").each(function() {

            if(!this.value){this.value = 0;}

            if(jQuery.isNumeric(this.value) && this.value>=0) {
                jQuery(this).parent().children('.cal-error').slideUp(300)
            }else{
                result = false;
                jQuery(this).parent().children('.cal-error').slideDown(300);
                jQuery('.cal-result').slideUp(300);
            }
        });
        return result;
    }

basically the problem is if(!this.value){this.value = 0;} if there is no value i need to set it to zero before carrying on with the validating the form, but this only seems to take effect the second time i validate it.

So it all boils down to if i set an inputs value to 0 in the each loop, it only takes it into account after the function is done.

Here is a JSFIDDLE, Identical to my current problem, only on the second click will the proper value appear.

Any Help Greatly Appreciated.

Simple Question I just cant solve, I have the following function:

function finalCheck(){

        var result = true;
        jQuery("#carloan-form :input[type=text]").each(function() {

            if(!this.value){this.value = 0;}

            if(jQuery.isNumeric(this.value) && this.value>=0) {
                jQuery(this).parent().children('.cal-error').slideUp(300)
            }else{
                result = false;
                jQuery(this).parent().children('.cal-error').slideDown(300);
                jQuery('.cal-result').slideUp(300);
            }
        });
        return result;
    }

basically the problem is if(!this.value){this.value = 0;} if there is no value i need to set it to zero before carrying on with the validating the form, but this only seems to take effect the second time i validate it.

So it all boils down to if i set an inputs value to 0 in the each loop, it only takes it into account after the function is done.

Here is a JSFIDDLE, Identical to my current problem, only on the second click will the proper value appear.

Any Help Greatly Appreciated.

Share Improve this question edited Nov 28, 2013 at 23:00 asked Nov 28, 2013 at 22:06 user1278673user1278673 10
  • Is this (finalCheck) your validate function function, if so, then just do it before you call it. – The Alpha Commented Nov 28, 2013 at 22:13
  • I need it inside my loop though. – user1278673 Commented Nov 28, 2013 at 22:13
  • Not sure if I really understood your question. – The Alpha Commented Nov 28, 2013 at 22:15
  • My Apologies, Do you understand it now? – user1278673 Commented Nov 28, 2013 at 22:16
  • 1 That's because you are setting finalValue before calling test(). I've moved that line, and it now sets "0" on the first click. jsfiddle/bQSA3/4 – Snixtor Commented Nov 28, 2013 at 23:03
 |  Show 5 more ments

4 Answers 4

Reset to default 9

You can set a default value for your print out

var finalValue = $('#test').val() || 0;

Check http://jsfiddle/odiseo/bQSA3/5/

The issue is when you are getting the value, though this isn't evident in your code snippet, only the JSFiddle. So where you've got this:

var finalValue = $('#test').val();
var testInput = test();
if(testInput){
     $('span').text(finalValue);
}

It instead needs to be this:

var testInput = test();
if(testInput){
    var finalValue = $('#test').val();
     $('span').text(finalValue);
}

Note the movement of setting finalValue to be after the call to test().

Working fiddle - http://jsfiddle/bQSA3/4/

Perhaps where you were mistaken was thinking the call to $('#test').val() set a reference to the value of the test input. It instead gets a copy of the value. If the value in test changes, you need to fetch the value again.

Try putting this...

if(!this.value){this.val(0);}

The below code (simplified) always displays true in alert, which I think is the expected behavior.

Html

<form id="carloan-form">
  <input type="text" />
  <input type="text" />
  <input type="text" />
  <button id="check">check</button>
</form>

Script

$(function () {
    $("#check").click(function() {
        var result = true;
        jQuery("#carloan-form :input[type=text]").each(function() {
            if(!this.value){this.value = 0;}

            if(jQuery.isNumeric(this.value) && this.value>=0) {

            }else{
                result = false;
            }
        });

        alert(result);
    });
});

本文标签: javascriptjQuery if value is empty set it to 0 and validateStack Overflow