【问题标题】:SyntaxError: missing ) after argument list in jquery whilst appendingSyntaxError: missing ) 在 jquery 中的参数列表之后同时追加
【发布时间】:2017-06-02 23:31:06
【问题描述】:

我收到此错误!缺少),即使我的 jquery 行似乎没有任何问题。

$('.product_area').append('
 <div class="product">
   <a href="/index.php?store='+$store_username+'&view=single&product='+json[i].product_id'"><div class="product-image">
    <img src="<?php echo image_check('+json[i].product_image1+'); ?>" />
    </div>
    </a>
  <div class="product_details"><div class="product-name">
 <a class="name" href="/index.php?store='+$store_username+'&view=single&product='+json[i].product_id'">
 <span><?php echo substr('+json[i].product_name+',0,23); ?></span>
 </a>
 </div>
  <div class="product-price">
 <span class="price">@ Rs. <?php echo number_format('+json[i].product_price+',2); ?></span>/-</div>
  <div class="product-discount">
 <span class="discount">Discount: 
 <span class="color-text"><?php echo '+json[i].product_discount+' ?></span>%</span>
 </div>
 </div>
 </div>').animate({width:'toggle'},150);

我尽量写得干净利落。任何人都可以检查!很烦人

【问题讨论】:

  • 您不能将字符串拆分为多行。您需要将其连接起来,或在行尾使用 \。您还试图将 JS 数据附加到 PHP 中,这将永远无法正常工作。问题的原因是在json[i].product_id 之后缺少+。投票结束是一个错字。
  • 你不能在` '' `或` "" `之间换行。
  • 这看起来很混乱

标签: javascript jquery


【解决方案1】:

这看起来像是将字符串拆分为多行的问题。 (正如其他人所评论的那样,您还有一个错字。如果这是您的代码中的一个错误,您需要修复它,但如果它只是这里的一个错字,那么这就是您面临的另一个问题)

有几种方法可以解决这个问题。

1) 如果您可以使用 ES6,请考虑使用带有 ``(反引号)的字符串模板。此解决方案可能如下所示:

let html = `<div class="product">
           <a href="/index.php?store=${store_username}&view=single&product...`

请注意,您不需要在此解决方案中使用 +,您只需使用 ${var_name} 即可获取变量的值。您也可以分成多行并且没问题。我认为您也可以将 append() 方法中的整个字符串替换为字符串模板就可以了。

2) 使用 += 运算符将您的 HTML 预打包到一个变量中,然后再附加它。这里可能看起来像这样:

var html = '<div class="product">';
html += '<a href="/index.php?store=';
html +=  $store_username;
html +=  '&view=single&product=';

等等,然后你会

.append(html);

3) 最后,您可以使用 \

分割行
... .append('<div class="product"> \
           <a href="/index.php?store= \
           '+$store_username+'&view=single&product=' ... );

【讨论】:

    【解决方案2】:

    改变这一行

    <a href="/index.php?store='+$store_username+'&view=single&product='+json[i].product_id'"><div class="product-image">
    

    <a href="/index.php?store='+$store_username+'&view=single&product='+json[i].product_id+'"><div class="product-image">
    

    【讨论】:

      【解决方案3】:

      这个怎么样?

      $('.product_area').append('<div class="product">\
          <a href="/index.php?store=\'+$store_username+\'&view=single&product=\'+json[i].product_id\'"><div class="product-image">\
              <img src="<?php echo image_check(\'+json[i].product_image1+\'); ?>" />\
              </div>\
              </a>\
          <div class="product_details"><div class="product-name">\
          <a class="name" href="/index.php?store=\'+$store_username+\'&view=single&product=\'+json[i].product_id\'">\
          <span><?php echo substr(\'+json[i].product_name+\',0,23); ?></span>\
          </a>\
          </div>\
          <div class="product-price">\
          <span class="price">@ Rs. <?php echo number_format(\'+json[i].product_price+\',2); ?></span>/-</div>\
          <div class="product-discount">\
          <span class="discount">Discount: \
          <span class="color-text"><?php echo \'+json[i].product_discount+\' ?>    </span>%</span>\
          </div>\
          </div>\
          </div>').animate({width:'toggle'},150);
      

      【讨论】:

      • SyntaxError:JSON.parse:JSON 数据的第 1 行第 2 列出现意外字符
      • \'+json[i].product_price+\' 可能不应该被转义。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      • 2018-06-15
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      相关资源
      最近更新 更多