Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.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下拉列表值_Jquery_Drop Down Menu - Fatal编程技术网

JQuery下拉列表值

JQuery下拉列表值,jquery,drop-down-menu,Jquery,Drop Down Menu,我有一个下拉列表,其中有一个项目列表,第一个项目的值为0,其他项目的值大于0 以下代码附加到更改事件: <script type="text/javascript"> $("#SearchRegionId").change(function (e) { var select = $("#SearchKommuneId"); select.empty(); if ($("#SearchRegionId").val() != 0);

我有一个下拉列表,其中有一个项目列表,第一个项目的值为0,其他项目的值大于0

以下代码附加到更改事件:

<script type="text/javascript">
    $("#SearchRegionId").change(function (e) {
        var select = $("#SearchKommuneId");
        select.empty();
        if ($("#SearchRegionId").val() != 0);
        {
            $.ajax({
                url: '/Kommune/getKommunerIRegion/',
                type: 'POST',
                data: { RegionId: $("#SearchRegionId").val() },
                dataType: 'json',
                success: function (data) {
                    for (i in data) {
                        select.append($('<option value="' + data[i].KommuneId + '">' + data[i].KommuneNavn + '</option>'));
                    }
                }
            });
        }
    });
</script>

$(“#SearchRegionId”)。更改(函数(e){
var select=$(“#SearchKommuneId”);
select.empty();
如果($(“#SearchRegionId”).val()!=0);
{
$.ajax({
url:“/Kommune/getkommunieriregion/”,
键入:“POST”,
数据:{RegionId:$(“#SearchRegionId”).val(),
数据类型:“json”,
成功:功能(数据){
对于(数据中的i){
select.append($(''+数据[i].KommuneNavn+'');
}
}
});
}
});
我的问题是,当我测试$(“#SearchRegionId”).val()时0即使选定的值为0,它也始终为真。我已经在一个显示值0的警报框中显示了该值,但有些东西告诉我它实际上不是0。

为什么要写

select.empty();
赋值后,再检查它

它使得

var select;
变成空的,不是吗?
这就是我的想法,就像你有一个

if ($("#SearchRegionId").val() != 0)
的末尾
表示if条件在此结束,之后的代码块将被执行,而与条件的值无关

<script type="text/javascript">
    $("#SearchRegionId").change(function (e) {
        var select = $("#SearchKommuneId");
        select.empty();
        if ($("#SearchRegionId").val() != 0)
        {
            $.ajax({
                url: '/Kommune/getKommunerIRegion/',
                type: 'POST',
                data: { RegionId: $("#SearchRegionId").val() },
                dataType: 'json',
                success: function (data) {
                    for (i in data) {
                        select.append($('<option value="' + data[i].KommuneId + '">' + data[i].KommuneNavn + '</option>'));
                    }
                }
            });
        }
    });
</script>

$(“#SearchRegionId”)。更改(函数(e){
var select=$(“#SearchKommuneId”);
select.empty();
if($(“#SearchRegionId”).val()!=0)
{
$.ajax({
url:“/Kommune/getkommunieriregion/”,
键入:“POST”,
数据:{RegionId:$(“#SearchRegionId”).val(),
数据类型:“json”,
成功:功能(数据){
对于(数据中的i){
select.append($(''+数据[i].KommuneNavn+'');
}
}
});
}
});
演示:

演示:

select.empty()为什么?请注意,在深入分析场景中,我有两个下拉列表:一个下拉列表用于市政:var select=$(“#SearchKommuneId”);select.empty();一个是区域,这是触发更改事件的下拉列表:if($(“#SearchRegionId”).val()!=“0”);阿伦·P·约翰尼,哦,我的……,非常感谢你的回答。我想我需要在阳光和风中旅行