admin管理员组

文章数量:1435859

I tried to do an "if/then" statement in javascript but the "then" mand is being ignored. Any ideas on why?

Specifically I want the form to be validated that the two text boxes are not blank and once that is validated I want a DIV ID = section2 to appear.

function checkanswers() {
    var fdet = document.wastheform;

    if (fdet.question_type[0].checked) {
        var elements = new Array("name","address");
        var elements_name = new Array("name", "address");
        for (var i = 0; i < elements.length; i++) {
            if ($("#" + elements[i]).val() == "") {
                err = "Please enter " + elements_name[i] + "";
                alert(err);
                return false;
            }
        }
        $("#section2").show();
    }

I tried to do an "if/then" statement in javascript but the "then" mand is being ignored. Any ideas on why?

Specifically I want the form to be validated that the two text boxes are not blank and once that is validated I want a DIV ID = section2 to appear.

function checkanswers() {
    var fdet = document.wastheform;

    if (fdet.question_type[0].checked) {
        var elements = new Array("name","address");
        var elements_name = new Array("name", "address");
        for (var i = 0; i < elements.length; i++) {
            if ($("#" + elements[i]).val() == "") {
                err = "Please enter " + elements_name[i] + "";
                alert(err);
                return false;
            }
        }
        $("#section2").show();
    }
Share Improve this question edited Feb 21, 2013 at 16:01 Konstantin Dinev 35k14 gold badges79 silver badges102 bronze badges asked Feb 21, 2013 at 16:00 John MontagueJohn Montague 951 gold badge5 silver badges15 bronze badges 3
  • You have two if here. Which one doesn't seem to work ? And how did you chek it ? – Denys Séguret Commented Feb 21, 2013 at 16:01
  • Aren't you confusing names and ids ? – Denys Séguret Commented Feb 21, 2013 at 16:02
  • 1 set a break point on the if statements and then use "add watch" to evaluate the different parts of the expression inside the if statement to determine why the left side doesn't equal the right side – Brandon Commented Feb 21, 2013 at 16:04
Add a ment  | 

2 Answers 2

Reset to default 0

I'm assuming your "then" is the showing of the div. In which case I would do something like this:

function checkanswers() {
    var fdet = document.wastheform;

    if (fdet.question_type[0].checked) {
        if(withoutValue("#name")) {
            alert("Please enter name")
        }
        else if(withoutValue("#address")) {
            alert("Please enter address")
        } else {
            $("#section2").show();
        }

    }
}

function withoutValue(selector) {
    return $(selector).val() === "";
}

Is that what you're looking for?

Your question implies that you want an if/then behavior (in Javascript, this is more properly referred to as an if/else statement), but the way you structured your code, there is no "then". Here is how the if statement should work:

if ( condition )
{
    // do something if the condition is true
}
else
{
    // do something if the condition is false
}

本文标签: if then statement javascriptStack Overflow