javascript Jquery 检查元素是否存在以及条件是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9805520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:54:28  来源:igfitidea点击:

Jquery check if element exist and if conditon

phpjavascriptjquery

提问by Sasha

I have code like this:

我有这样的代码:

<?php if ($this->session->userdata('permission') == 4 || $this->session->userdata('permission') == 3) : ?>
            <select name="caffe" id="caffe">
                <?php foreach($caffe as $key) : ?>
                <option value="<?php echo $key['id_caffe'] ?>"><?php echo $key['name'] ?></option>
                <?php endforeach; ?>
            </select>
            <div id="gll_select">
                <script>
                 var caffe = $("#caffe").val();
                 var cff_name = $("#caffe option:first").text();
                 $("#gll_select").load("<?php echo base_url() ?>form/galleries", {id : caffe}, function(){                 
                          $("#gll").on('change', function(){
            var cff_name  = $("#caffe option:selected").text();
            get_gallery(cff_name);
            console.log(cff_name);
        }); 
                 });  
                </script>
            </div>

            <?php else : ?>

            <input type="hidden" name="caffe_name" id="caffe_name" value="<?php echo $caffe_name ?>" />
            <input type="hidden" name="caffe_id" id="caffe_id" value="<?php  if(isset($caffe_id)) echo $caffe_id; ?>" />
            <div id="gll_select">
                <?php if (isset($gallery)) : ?>
            <select name="gll" id="gll">
        <?php foreach ($gallery as $key) : ?>
            <option value="<?php echo $key['id_gallery'] . " " . $key['name'] ?>"><?php echo $key['name'] ?></option>
        <?php endforeach; ?>
            <?php endif; ?>
            </select>
            </div>

            <?php endif ?> 

and jquery code like this:

和 jquery 代码是这样的:

if ($("#caffe".length)){var cff_name = $("#caffe option:first").text(); }
    else{var cff_name = $("#caffe_name").val();}

Problem is following - the first statement is always true (#caffewill not exist if PHP IF statement is FALSE). What seems to be the problem?

问题如下 - 第一个语句始终为真(如果#caffe将不存在PHP IF statement is FALSE)。似乎是什么问题?

回答by Pointy

Should be

应该

if ($("#caffe").length) { ... }

Your code:

您的代码:

if ($("#caffe".length)) { ... }

is getting the length of a string constant and passing that to jQuery. You always get a non-null result from a call to the jQuery main function ($), so the result of casting that to boolean as part of the ifstatement is always true.

正在获取字符串常量的长度并将其传递给 jQuery。您总是从对 jQuery 主函数 ($) 的调用中获得非空结果,因此将其转换为 boolean 作为if语句的一部分的结果始终为真。

When you instead test the .lengthproperty of the returned jQuery object made from a search for the selector, you're checking to see whether that number is non-zero.

当您测试通过.length搜索选择器生成的返回 jQuery 对象的属性时,您正在检查该数字是否为非零。

回答by safarov

Change $("#caffe".length)to $("#caffe").length

更改$("#caffe".length)$("#caffe").length

回答by Surreal Dreams

I think I see the problem...

我想我看到了问题...

if ($("#caffe").length){var cff_name = $("#caffe option:first").text(); }

.lengthis not part of the selector, it's an attribute of the wrapped set.

.length不是选择器的一部分,它是包装集的一个属性。