简体   繁体   English

javascript转义引号和vars

[英]javascript escape quotes and vars

I have the following piece of code that appends on different divs data from mysql but I have a problem with adding the image I can't escape the " correctly. 我有以下代码,它们会附加到来自mysql的不同divs数据上,但是我在添加无法正确转义“的图像时遇到问题。

   $(document).on("click", ".active-modal", function () {
     var queryId = $(this).data('uri');
     $.ajax({
       url: '/catalog/product/modal-ajax',
       type: 'POST',
       dataType: 'json',
       async: true,
       data: {productId: queryId},
       success: function(data) {
          for (var key in data) {
            $('.product-name').text(data.ProductName);
            $('.product-code').text(data.BexProductCode);
            $('<img src="img/door-right.png">').appendTo(".ball_footballbox");
            $('<img src="/public/uploads/productPictures/thumbnails/" '+ data.ProductImage +' ">');
          }
       }
     })
     .fail(function() {
       console.log("error");
     })
   });

If I understood your problem correctly, you need to change this line: 如果我正确理解了您的问题,则需要更改此行:

$('<img src="/public/uploads/productPictures/thumbnails/" '+ data.ProductImage +' ">');

to this one: 对此:

$('<img src="/public/uploads/productPictures/thumbnails/'+data.ProductImage+'">');

If data.ProductImage contains some quotes try to replace any " occurrence by &quot; entity and then paste in into src attribute. 如果data.ProductImage包含一些引号,请尝试用“”实体替换任何“出现的内容”,然后粘贴到src属性中。

Just change this: 只需更改此:

$('<img src="/public/uploads/productPictures/thumbnails/" '+ data.ProductImage +' ">');

to this: 对此:

$('<img src="/public/uploads/productPictures/thumbnails/'+ data.ProductImage + '" />').appendTo('#some-destination');

or you can put the src like this: 或者您可以像这样放置src:

var image_source = '/myimage/dir/' + data.ProductImage;

$('<img />').attr('src', image_source).appendTo('#some-holder');

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

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