简体   繁体   English

为什么我的elseif语句每次都运行

[英]Why is my elseif statement running everytime

I am building a form sending function in JavaScript, and I have run into a problem where it executes an else if statement every time. 我正在用JavaScript构建一个表单发送功能,并且遇到了每次执行else if语句的问题。 Here is my script: 这是我的脚本:

this.submit = function() {
    var url = this.url + "?";
    for(el in this.elements) {
        var e = $(this.elements[el]);
        var n = this.names[el];
        if(n === "submit")
        {
            window.alert("submit");
        }
        else
        {
        url += n + "=";
        }
        if(el == "#dropdown")
        {
            var options = e.children();
            for(var i = 0; i < options.length; i++) {
                var option = $('#' + options[i].id);
                if(option.attr('selected'))
                {
                    url += option.attr('name');
                    url += "&";
                    window.alert("dropdown worked");
                    break;
                }
            }
        }
        else if(el != "#submit") {
            url += e.attr('value');
            url += "&";
            window.alert("input worked");
        }
    }
    window.location.href = url;
};

The problem being that the else if(el != "#submit"){} runs even when the id in question is "#submit". 问题是,即使有问题的id是“ #submit”, else if(el != "#submit"){}也会运行。 Does anyone know why this doesn't work? 有谁知道为什么这不起作用?

To help, here is my php document, and the rest of the form constructer: 为了提供帮助,这是我的PHP文档以及其余的表单构造函数:

php: 的PHP:

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<?php if(!$_GET): ?>
<form id="form1">
    <input type="text" id="input1" name="name"/>
    <br>
    <select id="dropdown" name="color">
        <option id="null" name="null"></option>
        <option id="opt1" name="blue">blue</option>
        <option id="opt2" name="yellow">yellow</option>
    </select>
    <br>
    <button type="submit" id="submit" name="submit"onclick="form1.submit()">Submit data</button>
</form>
<script src="http://charlie/form.js"></script>
<script>
    var form1 = new form("form1");
    form1.setElements();
    form1.logElements();
</script>
<?php elseif(!(isset($_GET['name']) || isset($_GET['color']))): ?>
<p style="color:red">ERROR! form.js failed</p>
<?php else: ?>
<p><?= $_GET['name'] ?></p>
<p><?= $_GET['color'] ?></p>
<?php endif; ?>
</body>
</html>

form constructer: 表单构造器:

function form(id) {
this.id = "#" + id;
this.url = window.location.href;
this.elements = [];
this.names = [];
this.setElements = function() {
    var elements = [],names = [],children = $(this.id).children();
    for(var i = 0; i < children.length; i++) {
        var childid = children[i].id;
        if(childid)
        {
            elements.push('#' + childid);
        }
    }
    this.elements = elements;
    for(var e in this.elements) {
        names.push($(this.elements[e]).attr('name'));
    }
    this.names = names;
};
this.logElements = function() {
    for(var e in this.elements) {
        console.log(this.elements[e]);
    }
    for(var n in this.names) {
        console.log(this.names[n]);
    }
};
this.submit = function() {
    var url = this.url + "?";
    for(el in this.elements) {
        var e = $(this.elements[el]);
        var n = this.names[el];
        if(n === "submit")
        {
            window.alert("submit");
        }
        else
        {
        url += n + "=";
        }
        if(el == "#dropdown")
        {
            var options = e.children();
            for(var i = 0; i < options.length; i++) {
                var option = $('#' + options[i].id);
                if(option.attr('selected'))
                {
                    url += option.attr('name');
                    url += "&";
                    window.alert("dropdown worked");
                    break;
                }
            }
        }
        else if(el != "#submit") {
            url += e.attr('value');
            url += "&";
            window.alert("input worked");
        }
    }
    window.location.href = url;
};
}

Turning my comment into an answer with some code. 用一些代码将我的评论变成答案。 The "in" operator in Javascript iterates over properties not the elements at each index. Javascript中的“ in”运算符遍历属性,而不是遍历每个索引的元素。 To make your current code work, change the code to the following: 要使当前代码正常工作,请将代码更改为以下内容:

var el;
var elementCount = this.elements.length;
for (var i = 0; i < elementCount; i++) {
    el = this.elements[i];

This will produce the expected behavior. 这将产生预期的行为。

The for...in loop is the cause. for ... in循环是原因。 el tkaes the values 0, 1, 2 ...you need to compare this.elements[el] instead of el : el的值分别为0、1、2 ...您需要比较this.elements[el]而不是el

if(this.elements[el] == "#dropdown") ... 
else if(this.elements[el] != "#submit") {

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM