Javascript 使用 jQuery 检查 ID 是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11600479/
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-08-24 06:30:03  来源:igfitidea点击:

Check if an ID exists or not using jQuery

javascriptjquery

提问by stackminu

Possible Duplicate:
Finding whether the element exists in whole html page

可能的重复:
查找元素是否存在于整个 html 页面中

Is there any way to check for if a ID $('#ID') exists in jQuery

有什么方法可以检查 jQuery 中是否存在 ID $('#ID')

Ex:

前任:

$('#cart').append('<li id="456">My Product</li>');

$('#cart').append('<li id="456">My Product</li>');

After running append() to something like this I want to check if my ID $('#456') exists or not. If it exits I want to change the text, else I want to append a new

在对这样的事情运行 append() 之后,我想检查我的 ID $('#456') 是否存在。如果它退出我想更改文本,否则我想附加一个新的

  • .

    $(document).ready(function() {
            $('#RAM,#HDD').change(function() {
    
                var product = $(this).find("option:selected").attr("product");
    
                $.ajax({
                    url: 'ajax.php',
                    type: 'post',
                    data: $(this).serialize(),
                    dataType: 'json',
                    success: function(data) {
                        $('#cart').empty();
                        $.each(data, function(index, value) {
                            $('#cart').append('<li id="'+product+'">'+ value['price'] +'</li>');
                        });
                    }
                });
            });
        });
    
  • .

    $(document).ready(function() {
            $('#RAM,#HDD').change(function() {
    
                var product = $(this).find("option:selected").attr("product");
    
                $.ajax({
                    url: 'ajax.php',
                    type: 'post',
                    data: $(this).serialize(),
                    dataType: 'json',
                    success: function(data) {
                        $('#cart').empty();
                        $.each(data, function(index, value) {
                            $('#cart').append('<li id="'+product+'">'+ value['price'] +'</li>');
                        });
                    }
                });
            });
        });
    
  • 回答by Paul Fleming

    if ($('#456').length)
    {
     /* it exists */
    }
    else
    {
     /* it doesn't exist */
    }
    

    回答by Tarik

    You can do this to see if the selector does exist or not:

    您可以这样做以查看选择器是否存在:

    jQuery.fn.exists = function(){return this.length>0;}
    
    if ($(selector).exists()) {
        // Do something
    }
    

    回答by Will Morgan

    Erm, I'm not sure why you would want to do that, but in order to do so you will need to rearrange your code slightly.

    嗯,我不确定你为什么要这样做,但为了这样做,你需要稍微重新排列你的代码。

    success: function(data) {
        var cart = $('#cart');
        cart.empty();
        $.each(data, function(index, value) {
            cart.append('<li id="'+product+'">'+ value['price'] +'</li>');
        });
        if(cart.find('#456').length) {
            cart.find('#456').text('Whatever you would like');
        }
    }
    

    I 'cached' your cart selector to save on DOM lookups.

    我“缓存”了您的购物车选择器以节省 DOM 查找。

    回答by thecodeparadox

    function checkExists(sel) {
        var status = false;
        if ($(sel).length) status = true;
        return status;
    }
    

    Working sample

    工作样本