jquery - 使用AJAX和PHP函数更新图像src

标签 jquery ajax wordpress woocommerce product-variations

我想在 WordPress 中使用 AJAX 更改点击功能上的图像。 这是我的代码:

JS:

jQuery(".swatchinput label").click(function(){
     var mycolor = jQuery(this).attr("data-option");
     var postid = jQuery(".single-product .product.type-product").attr("data-id");

        jQuery.ajax({
            cache: false,
            timeout: 8000,
            url: php_array.admin_ajax,
            type: "POST",
            data: ({ action:'theme_post_vimage', colorimg: mycolor, postvalue: postid}),

                  beforeSend: function() {                    
            },

            success: function(response){

                var myimageresponse = jQuery( response );
                jQuery( '.product-image a img' ).attr('src', myimageresponse);                                                      

            },

            error: function( jqXHR, textStatus, errorThrown ){
                console.log( 'The following error occured: ' + textStatus, errorThrown );   
            },

            complete: function( jqXHR, textStatus ){
            }


            });
    });

这是我在 PHP/WordPress 中的函数:

add_action('wp_ajax_theme_post_vimage','theme_post_vimage_init');
add_action( 'wp_ajax_nopriv_theme_post_vimage', 'theme_post_vimage_init' );
function theme_post_vimage_init() { ?>
<?php
global $post, $product, $woocommerce;    

 $postiID = $_POST['postvalue'];
 $colorname = $_POST['colorimg'];


$product = new WC_Product_Variable( $postiID );
$variations = $product->get_available_variations();
foreach ( $variations as $variation ) {
    if($variation['attributes']['attribute_pa_color'] == $colorname) :
    $myimageurl = $variation['image']['url'];
    echo $myimageurl;
    endif;
}
?>

<?php }

当我点击我的颜色时,此错误显示在我的浏览器控制台中:

Uncaught Error: Syntax error, unrecognized expression: http://samsonite.stuntmen.ae/wp-content/uploads/2017/05/PROD_COL_73353_1726_WHEEL-HANDLE-FULL.jpg

最佳答案

jQuery( response ); 是错误的...为什么不直接使用 response

        success: function(response){

            jQuery( '.product-image a img' ).attr('src', response);                                                      

        },

此外,我建议您将实现更改为类似这样的内容。

PHP

function theme_post_vimage_init() { 

    $postiID = $_POST['postvalue'];
    $colorname = $_POST['colorimg'];


    $product = wc_get_product( $postiID );
    $variations = $product->get_available_variations();
    $return = array(
        'status' => 'failed',
    );
    foreach ( $variations as $variation ) {
        if($variation['attributes']['attribute_pa_color'] == $colorname) :
            $return = array(
                'status' => 'success',
                'url' => $variation['image']['url'],
            );
        endif;
    }
    wp_send_json($return);
}

jQuery

jQuery(".swatchinput label").click(function(){
     var mycolor = jQuery(this).attr("data-option");
     var postid = jQuery(".single-product .product.type-product").attr("data-id");

    jQuery.ajax({
        cache: false,
        timeout: 8000,
        url: php_array.admin_ajax,
        type: "POST",
        data: ({ action:'theme_post_vimage', colorimg: mycolor, postvalue: postid}),

              beforeSend: function() {                    
        },

        success: function(response){
            if ( response.status === 'success' ) {
               jQuery( '.product-image a img' ).attr('src', response.url);   
            }
            // you can also do something for response.status === 'failed'                    

        },

        error: function( jqXHR, textStatus, errorThrown ){
            console.log( 'The following error occured: ' + textStatus, errorThrown );   
        },

        complete: function( jqXHR, textStatus ){
        }


        });
});

寻找成功我做了改变。

关于jquery - 使用AJAX和PHP函数更新图像src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44153705/

相关文章:

javascript - 保护 DIV 元素不在 TinyMCE 中被删除

javascript - 在不使用 css 文本转换的情况下,在用户键入时将文本字段中的文本大写?

javascript - 在wordpress中添加弹出窗口(add_records.php)

javascript - 尝试按顺序运行 javascript AJAX 调用

mysql - 更新多个值的 SQL 查询

wordpress 中的 Mysql_fetch_array

javascript - each、ajax、promise 并以正确的方式设置 html

Javascript DIV 保留在任何表格的右上角

javascript - 如何动态地将新行添加到表中,每行都有表标题和列?

javascript - Python - 从服务器获取数据(ajax) - 'str'对象不可调用错误