Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery问题,If语句工作不正常_Jquery_If Statement - Fatal编程技术网

jQuery问题,If语句工作不正常

jQuery问题,If语句工作不正常,jquery,if-statement,Jquery,If Statement,我有以下代码: $(document).ready(function(){ $("[name=q58]").each(function (i) { $(this).click(function () { var selection = $(this).val(); alert('Q58 Value = '+selection) if (selection == '1') {

我有以下代码:

$(document).ready(function(){

    $("[name=q58]").each(function (i) {
        $(this).click(function () {
            var selection = $(this).val();
            alert('Q58 Value = '+selection)
            if (selection == '1') {
                $("#region").show();

                $("[name=q59]").each(function (i) {
                    $(this).click(function () {
                        var selection = $(this).val();
                        alert('Q59 Value = '+selection)
                        if (selection == '1') {
                            $("#location").show();
                            $("input:radio[name^='east-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='north-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='scotland-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='southeast-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='southwest-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("#east, #north, #scotland, #southeast, #southwest").hide();
                            $("#central").show();
                        }
                        else if (selection == '2') {
                            $("#location").show();
                            $("input:radio[name^='central-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='north-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='scotland-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='southeast-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='southwest-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("#central, #north, #scotland, #southeast, #southwest").hide();
                            $("#east").show();
                        }
                        else if (selection == '3') {
                            $("#location").show();
                            $("input:radio[name^='central-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='east-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='scotland-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='southeast-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='southwest-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("#central, #east, #scotland, #southeast, #southwest").hide();
                            $("#north").show();
                        }
                        else if (selection == '4') {
                            $("#location").show();
                            $("input:radio[name^='central-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='east-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='north-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='southeast-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='southwest-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("#central, #east, #north, #southeast, #southwest").hide();
                            $("#scotland").show();
                        }
                        else if (selection == '5') {
                            $("#location").show();
                            $("input:radio[name^='central-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='east-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='north-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='scotland-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='southwest-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("#central, #east, #north, #scotland, #southwest").hide();
                            $("#southeast").show();
                        }
                        else if (selection == '6') {
                            $("#location").show();
                            $("input:radio[name^='central-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='east-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='north-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='scotland-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='southeast-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("#central, #east, #north, #scotland, #southeast").hide();
                            $("#southwest").show();
                        }
                        else {
                            $("#location").hide();
                            $("input:radio[name^='central-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='east-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='north-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='scotland-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='southeast-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("input:radio[name^='southwest-']").each(function(i) {
                                   this.checked = false;
                            });
                            $("#central, #east, #north, #scotland, #southeast, #southwest").hide();
                        }             
                    });
                });

            }
            else if (selection == '10') {
                $("input:radio[name^='central-']").each(function(i) {
                       this.checked = false;
                });
                $("input:radio[name^='east-']").each(function(i) {
                       this.checked = false;
                });
                $("input:radio[name^='north-']").each(function(i) {
                       this.checked = false;
                });
                $("input:radio[name^='scotland-']").each(function(i) {
                       this.checked = false;
                });
                $("input:radio[name^='southeast-']").each(function(i) {
                       this.checked = false;
                });
                $("input:radio[name^='southwest-']").each(function(i) {
                       this.checked = false;
                });
                $("#central, #east, #north, #scotland, #southeast, #southwest").hide();
                $("#location").hide();
                $("#region").show();
            }
            else {
                $("input:radio[name^='q59']").each(function(i) {
                       this.checked = false;
                });
                $("input:radio[name^='central-']").each(function(i) {
                       this.checked = false;
                });
                $("input:radio[name^='east-']").each(function(i) {
                       this.checked = false;
                });
                $("input:radio[name^='north-']").each(function(i) {
                       this.checked = false;
                });
                $("input:radio[name^='scotland-']").each(function(i) {
                       this.checked = false;
                });
                $("input:radio[name^='southeast-']").each(function(i) {
                       this.checked = false;
                });
                $("input:radio[name^='southwest-']").each(function(i) {
                       this.checked = false;
                });
                $("#region").hide();
                $("#location").hide();
                $("#central").hide();
            }             
        });
    });




});
JSIDdle中的示例正在工作,或者更确切地说不是这样工作:请参见JSIDdle中的示例:

出现以下奇怪行为:

用户选择
Aftersales
,然后查看
q59
,可以选择
Central
East
,然后查看
q60
并选择其他选项。这个很好用

如果用户选择了
Bus、Coach和Engines
,则不会得到任何进一步的选项-同样可以正常工作

奇怪的行为是,当用户选择
Sales
时,如果他们选择此选项,那么他们应该能够选择
Central
East
——如果他们单击了
Sales
而没有其他选项,则此选项可以正常工作如果他们选择了
售后服务
,然后改变主意,选择了
销售
,然后选择
中部
东部
,则会显示
q60
,不应该显示


我一直在查看jQuery代码,看不出哪里出了问题。或者这是一个奇怪的现象?

问题在于事件绑定 如果是售后服务,您将点击事件绑定到q59。因此,当用户选择
Sales
并从q59中选择某个内容时,单击处理程序将执行。这是导致问题的一段代码:

$("[name=q59]").each(function (i) {
  $(this).click(function () { ...
您可以通过解除事件绑定或使用事件内部的某些条件来检查是否选择了
AfterSales
Sales
来避免此问题

解绑使用

$("[name=q59]").each(function (i) {
                $(this).unbind('click');
                });
在其他部分


请参阅工作

问题在于事件绑定 如果是售后服务,您将点击事件绑定到q59。因此,当用户选择
Sales
并从q59中选择某个内容时,单击处理程序将执行。这是导致问题的一段代码:

$("[name=q59]").each(function (i) {
  $(this).click(function () { ...
您可以通过解除事件绑定或使用事件内部的某些条件来检查是否选择了
AfterSales
Sales
来避免此问题

解绑使用

$("[name=q59]").each(function (i) {
                $(this).unbind('click');
                });
在其他部分


请参阅正在运行的代码,您的代码出现了一些逻辑问题,我将在下面修复尝试

$(文档).ready(函数(){

58 您在哪个领域工作: 售后服务 公共汽车、客车和发动机 销售额

59
您在哪个地区工作:
中心的
东边
60
您在哪个地点工作:
客户运营
部分
波士顿
唐卡斯特VMU

您的代码出现了一些逻辑问题,我在下面修复尝试

$(文档).ready(函数(){

58 您在哪个领域工作: 售后服务 公共汽车、客车和发动机 销售额

59
您在哪个地区工作:
中心的
东边
60
您在哪个地点工作:
客户运营
部分
波士顿
唐卡斯特VMU

我不明白的是,只有当用户选择了“售后服务”,代码才应该可用-如果他们选择了其他内容,代码就不应该启动。或者是因为一旦绑定,它就可以正常工作?不确定如何停止-任何建议?感谢您的输入。一旦单击处理程序与q59绑定,它就会执行e每次在q50中单击任何选项时。请参阅我的更新答案以获取解决方案谢谢-没有意识到一旦绑定它就会保持绑定-学到了一些新的东西-受到了赞赏。我不明白的是,只有当用户选择了
售后服务
,代码才可用-如果他们选择了任何其他内容,该代码不应该触发。或者是因为它一旦绑定就可以正常工作?不知道如何停止-有什么建议吗?感谢您的输入。一旦点击处理程序与q59绑定,它将在每次点击q50中的任何选项时执行。请参阅我的更新答案以获取解决方案谢谢-没有意识到一旦绑定它将保持绑定状态-学到了一些新的东西-appr感激的。