简体   繁体   English

用ajax添加后检查div是否具有特定文本

[英]Check if div has specific text after been added with ajax

I want to check if a specific div has an specific text that has been added with ajax. 我想检查特定的div是否具有已用ajax添加的特定文本。

This is the ajax function code: 这是ajax函数代码:

    $(function() {
        $(document).on('click', '#JS_couponSubmit', function(e) {
            e.preventDefault();
            var $this = $(this);
            $this.data('text', $this.text());
            $this.text('Cargando...');
            var $form = $('#JS_couponForm');

            $.ajax({
                url: $form.attr('data-action'),
                data: {
                    'coupon_code': $('#JS_couponCode').val(),
                    'view_cart': 'front_cart/v_cart'
                },
                type: 'post',
                dataType: 'json',
                cache: false,
                success: function(response) {
                    $this.text($this.data('text'));
                    $('#JS_cart').replaceWith(response.htmlCart);
                    $('#JS_couponResponse')
                    .html(response.message)
                    .addClass(response.message?'success':'error');
                    $('#JS_couponForm th p').addClass('active');
                    $('#JS_couponForm td').show(100);
                }
            });
        });
    });

This code adds the text No existe este código (or another text if the coupon code is correct but for the purpose of this question I just use this one) to #JS_couponResponse . 该代码将文本No existe este código (或其他文本如果优惠券代码是正确的,但对这个问题的目的,我只用这一个)来#JS_couponResponse

So I have tried using this function inside the success: function(response) { } but it simply does not work: 因此,我尝试在success: function(response) { }内部使用此函数success: function(response) { }但它根本不起作用:

setTimeout(function(){ 
  $('#JS_couponResponse:contains("No existe este código")').each(function () {
      alert("Has text");
  });
},1000);

I also tried this, again inside the success: function(response) { } : 我也尝试过这个,再次在success: function(response) { }里面success: function(response) { }

var $myDiv = $('#JS_couponResponse:contains("No existe este código")');
if ( $myDiv.length){
  alert("Has text");
}

Any idea how to do this? 任何想法如何做到这一点?

You can add use indexOf in your success method. 您可以在成功方法中添加使用indexOf

 $(document).ready(function(){ $('.JS_couponResponse').each(function(){ if($(this).text().indexOf("No existe este códig")>-1) { $(this).css("color","green"); } else { $(this).css("color","red"); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="JS_couponResponse"> No existe este códig efff fdg sdgs</div> <div class="JS_couponResponse">efff fdg sdgs</div> 

The problem with :contains is that it cannot cope with accented characters, like ó . :contains的问题在于它无法应付重音字符,例如ó

If you avoid those any of your current approaches will work as-is: 如果您避免使用这些方法,那么您当前的任何方法都将照常运行:

eg 例如

var $myDiv = $('#JS_couponResponse:contains("No existe este")');
if ( $myDiv.length){
  alert("Has text");
}

I would go for: 我会去:

        $.ajax({
            url: $form.attr('data-action'),
            data: {
                'coupon_code': $('#JS_couponCode').val(),
                'view_cart': 'front_cart/v_cart'
            },
            type: 'post',
            dataType: 'json',
            cache: false,
            success: function(response) {
                $this.text($this.data('text'));
                $('#JS_cart').replaceWith(response.htmlCart);
                $('#JS_couponResponse')
                .html(response.message)
                .addClass(response.message?'success':'error');
                $('#JS_couponForm th p').addClass('active');
                $('#JS_couponForm td').show(100);

                // Code to add in your success callback if you have several div ".couponResponse"
                $('.JS_couponResponse').each(function() {
                    if($(this).text() == 'No existe este código'))
                    {
                        alert('Has text');
                    }
                });

                // Code to add in your success callback if you have only one div "#couponResponse"
                if($('#JS_couponResponse').text() == 'No existe este código'))
                {
                    alert('Has text');
                }
        });

Note that you would need to switch from id="JS_couponResponse" to class="JS_couponResponse" if you have several div s. 请注意,如果您有多个div则需要从id="JS_couponResponse"切换到class="JS_couponResponse" HTML ID are unique inside the DOM. HTML ID在DOM内部是唯一的。

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

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