在ajax成功函数中获取错误。 getsaves.text不是一个函数

时间:2016-01-14 14:45:23

标签: javascript jquery ajax

这是我的代码

<script>jQuery('.appendurl').click(function(e){
                e.preventDefault();
                var getsaves = jQuery(this).text();
                var ajaxurl = '/wp-admin/admin-ajax.php';
                jQuery.post(
                    ajaxurl, 
                    {
                        'action': 'add_to_wishlist',
                        'add_to_wishlist': jQuery(this).attr('data-product-id'),   
                        'product_type': jQuery(this).attr('data-product-type')
                    }, 
                    function(response){
                        if(response.result = 'true'){
                            jQuery(this).closest('.yith-wcwl-add-button').removeClass('show');
                            jQuery(this).closest('.yith-wcwl-add-button').addClass('hide');
                            jQuery(this).closest('.yith-wcwl-wishlistexistsbrowse').removeClass('hide');
                            jQuery(this).closest('.yith-wcwl-wishlistexistsbrowse').addClass('show');
                            saves = getsaves.replace(' saves','');
                            countInc = parseInt(saves, 10) + 1;
                            updatedText = countInc + ' saves';
                            alert(updatedText);
                            getsaves.text(updatedText);
                        }
                    }
                );
            });</script>

我在ajax成功函数的最后一行得到了错误。错误是TypeError:getsaves.text不是函数getsaves.text(updatedText);

getsaves.text(updatedText)

在警报中,我获得了价值和相同的获取,我已经在该行上方使用了替换某些字符串。

1 个答案:

答案 0 :(得分:2)

$this = jQuery($this)是一个字符串,而不是jQuery对象,因此您无法在其上调用$this

我建议你指定this并用this替换后者的所有匹配项,然后在相关的行上使用它。

这也可以解决这些回调中的jQuery('.appendurl').click(function(e) { e.preventDefault(); var $this = jQuery(this); var getsaves = $this.text(); var ajaxurl = '/wp-admin/admin-ajax.php'; jQuery.post(ajaxurl, { 'action': 'add_to_wishlist', 'add_to_wishlist': $this.attr('data-product-id'), 'product_type': $this.attr('data-product-type') }, function(response) { if (response.result == 'true' ) { // fixed '=' error here! $this.closest('.yith-wcwl-add-button').removeClass('show'); $this.closest('.yith-wcwl-add-button').addClass('hide'); $this.closest('.yith-wcwl-wishlistexistsbrowse').removeClass('hide'); $this.closest('.yith-wcwl-wishlistexistsbrowse').addClass('show'); saves = getsaves.replace(' saves',''); countInc = parseInt(saves, 10) + 1; updatedText = countInc + ' saves'; // alert(updatedText); $this.text(updatedText); } }); }); 不是您认为的.removeClass的问题:

.addClass

您还可以链接两对$this.closest('.yith-wcwl-add-button').removeClass('show').addClass('hide'); char arr[10][50]={NULL}; int lines; //inserting values to arr for(int i=0;i<10;i++){ if(arr[i] == NULL){ lines++; printf("empty"); } else printf("not empty"); } 来电,例如:

{{1}}