簡體   English   中英

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

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

我想檢查特定的div是否具有已用ajax添加的特定文本。

這是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);
                }
            });
        });
    });

該代碼將文本No existe este código (或其他文本如果優惠券代碼是正確的,但對這個問題的目的,我只用這一個)來#JS_couponResponse

因此,我嘗試在success: function(response) { }內部使用此函數success: function(response) { }但它根本不起作用:

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

我也嘗試過這個,再次在success: function(response) { }里面success: function(response) { }

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

任何想法如何做到這一點?

您可以在成功方法中添加使用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> 

:contains的問題在於它無法應付重音字符,例如ó

如果您避免使用這些方法,那么您當前的任何方法都將照常運行:

例如

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

我會去:

        $.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');
                }
        });

請注意,如果您有多個div則需要從id="JS_couponResponse"切換到class="JS_couponResponse" HTML ID在DOM內部是唯一的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM