简体   繁体   English

由于 if-else 语句,JavaScript 函数没有被执行

[英]JavaScript Function is not getting executed because of if-else statements

I am developing a code where the services are coming from database into <li></li> tags.我正在开发一个代码,其中服务从数据库进入<li></li>标签。

Only few of the services are active currently and if an user selects a service and if that service is active then proceed and if not then an alert message should be displayed.当前只有少数服务处于活动状态,如果用户选择了一项服务,并且该服务处于活动状态,则继续,如果不是,则应显示警报消息。

I have no idea why do this function validation() not getting executed.我不知道为什么这个function validation()没有被执行。 Any help will be appreciated任何帮助将不胜感激

below is the code i am working on:下面是我正在处理的代码:

<script language="javascript">
    function validation() {
        var category = document.getElementById("category").value;
        category = category.trim();
        if (category == "") {
            alert("Please Select Category.");
            return;
        } else {
            var limiter = ["Electrician", "Photographer", "Printing Press", "Website Designer", "Web developer", "DJ Music", "Coaching Classes", "Civil Contractor", "Computer Hardware Services"];
            var limiter_count = limiter.length;
            for (var i = 0; i < limiter_count; i++) {
                if (category == limiter[i]) {
                    document.getElementById("form1").action = "categoryforms.php";
                    document.getElementById("form1").submit();
                    //loop should be terminated
                } else {
                    var alrt = "We will soon be launching our service in " + category + ", kindly stay tuned...";
                    alert(alrt);
                    //loop should be terminated here also
                }
            }
        }
    }
</script>

and the form form is:和表格形式是:

<form action="" method="post" name="form1" id="form1"  >
    <input type="text" name="category" id="category" value="<?php echo $category; ?>" />

    <ul id="categorymenu" class="mcdropdown_menu">

    <?php
        $sql = "SELECT * FROM  category";
        $sql_result = mysql_query ($sql);

        while ($row = mysql_fetch_assoc($sql_result)){
        ?>
            <li rel="<?php echo $row["category"]; ?>" value="<?php echo $row["category"]; ?>" ><?php echo $row["category"]; ?> </li>
            <?php
        }
    ?>
    </ul>

    <input id="button" type="button" value="Send "  class="serch" onClick="javascript:validation()">
</form>

You don't need for-loop, use Array.prototype.indexOf() to check element exists in array or not.您不需要 for 循环,使用Array.prototype.indexOf()来检查元素是否存在于数组中。

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present. indexOf()方法返回可以在数组中找到给定元素的第一个索引,如果不存在,则返回-1

Script脚本

function validation() {
    var category = document.getElementById("category").value;
    category = category.trim();

    if (category == "") {
        alert("Please Select Category.");
        return;
    }

    var limiter = ["Electrician", "Photographer", "Printing Press", "Website Designer", "Web developer", "DJ Music", "Coaching Classes", "Civil Contractor", "Computer Hardware Services"];

    var index = limiter.indexOf(category);
    if (index > -1) {
        document.getElementById("form1").action = "categoryforms.php";
        document.getElementById("form1").submit();
        //loop should be terminated

    } else {
        var alrt = "We will soon be launching our service in " + category + ", kindly stay tuned...";
        alert(alrt);
        //loop should be terminated here also
    }
}

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

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