Javascript 如何使用jquery从其他表中获取价格

Javascript 如何使用jquery从其他表中获取价格,javascript,jquery,json,laravel-4,Javascript,Jquery,Json,Laravel 4,如何从表产品选项中获取price和size\u name 产品选项表 -id -product_id -pricee -size_name 控制器: function addToCart() { $product_id = Input::get('product_id'); $quantity = 1; $product = product::find($product_id); if ($product) { $item = new Cart($product_id,

如何从表
产品选项中获取
price
size\u name

产品选项表

-id
-product_id
-pricee
-size_name
控制器:

function addToCart() {
  $product_id = Input::get('product_id');
  $quantity = 1;
  $product = product::find($product_id);
  if ($product) {
    $item = new Cart($product_id, $quantity);
    Session::push('cart', serialize($item));
    return Response::json(1);
  }
  return Response::json(0);
}

function myCart() {
  $cart_items = [];
  $session_items;
  if (Session::has('cart') && !empty(Session::get('cart'))) {
    $session_items = Session::get('cart');
    foreach($session_items as $key => $item) {
      $unserialized_item = unserialize($item);
      $product = product::find($unserialized_item - > productId);
      $cart_item = new StdClass;
      $cart_item - > cart_id = $key;
      $cart_item - > productId = $product - > id;
      $cart_item - > seller_id = $product - > seller_id;
      $cart_item - > category_id = $product - > category_id;
      $cart_item - > product_name = $product - > product_name;
      $cart_item - > description = $product - > description;
      $cart_item - > price = $product - > price;
      $cart_item - > image = $product - > image;
      $cart_item - > quantity = $unserialized_item - > quantity;
      $cart_item - > total = ($cart_item - > price * $cart_item - > quantity);
      $counter = 0;
      $found = false;
      $index = null;
      foreach($cart_items as $it) {
        if ($it - > productId == $product - > id) {
          $index = $counter;
          $found = true;
        }
        $counter++;
      }
      if ($found) {
        $cart_items[$index] - > quantity = $cart_items[$index] - > quantity + $cart_item - > quantity;
        $cart_items[$index] - > total = ($cart_items[$index] - > quantity * $cart_item - > price);
      } else {
        $cart_items[] = $cart_item;
      }
    }
    if (Request::ajax())
    return $cart_items;
    else
      return View::make('cart', ['cart_items' => $cart_items]);
  }
  if (Request::ajax())
  return $cart_items;
  else
    return View::make('cart', ['cart_items' => $cart_items]);
}
}
cartlist();

function cartList() {
  $.get("{{ URL::route('myCart') }}").done(function(data) {
    $(".dropdown-cartlist").html("");
    if (data.length == 0) {
      $(".dropdown-cartlist").html("<li><span class='item'><h4>No items on the cart.</h4></span></li>");
    } else {
      for (var i = 0; i < data.length; i++) {
        var cart_items = "<li>" +
            "<span class='item'>" +
            "<span class='item-left'>" +
            "<img width='50px' height='50px' src='{{ URL::asset('assets/images/uploads/') }}/" + data[i].image + "' alt='' />" +
            "<span class='item-info'>" +
            "<span>" + data[i].product_name + " x " + data[i].quantity + "($" + data[i].pricee + ")</span>" +
            "<span>$" + data[i].total + "</span>" +
            "</span>" +
            "</span>" +
            "<span class='item-right'>" +
            "<button data-id='" + data[i].cart_id + "' class='removeProduct btn btn-xs btn-danger pull-right'>x</button>" +
            "</span>" +
            "</span>" +
            "</li>";
        $(".dropdown-cartlist").append(cart_items);
      };
    }
  });
}
主页:

function addToCart() {
  $product_id = Input::get('product_id');
  $quantity = 1;
  $product = product::find($product_id);
  if ($product) {
    $item = new Cart($product_id, $quantity);
    Session::push('cart', serialize($item));
    return Response::json(1);
  }
  return Response::json(0);
}

function myCart() {
  $cart_items = [];
  $session_items;
  if (Session::has('cart') && !empty(Session::get('cart'))) {
    $session_items = Session::get('cart');
    foreach($session_items as $key => $item) {
      $unserialized_item = unserialize($item);
      $product = product::find($unserialized_item - > productId);
      $cart_item = new StdClass;
      $cart_item - > cart_id = $key;
      $cart_item - > productId = $product - > id;
      $cart_item - > seller_id = $product - > seller_id;
      $cart_item - > category_id = $product - > category_id;
      $cart_item - > product_name = $product - > product_name;
      $cart_item - > description = $product - > description;
      $cart_item - > price = $product - > price;
      $cart_item - > image = $product - > image;
      $cart_item - > quantity = $unserialized_item - > quantity;
      $cart_item - > total = ($cart_item - > price * $cart_item - > quantity);
      $counter = 0;
      $found = false;
      $index = null;
      foreach($cart_items as $it) {
        if ($it - > productId == $product - > id) {
          $index = $counter;
          $found = true;
        }
        $counter++;
      }
      if ($found) {
        $cart_items[$index] - > quantity = $cart_items[$index] - > quantity + $cart_item - > quantity;
        $cart_items[$index] - > total = ($cart_items[$index] - > quantity * $cart_item - > price);
      } else {
        $cart_items[] = $cart_item;
      }
    }
    if (Request::ajax())
    return $cart_items;
    else
      return View::make('cart', ['cart_items' => $cart_items]);
  }
  if (Request::ajax())
  return $cart_items;
  else
    return View::make('cart', ['cart_items' => $cart_items]);
}
}
cartlist();

function cartList() {
  $.get("{{ URL::route('myCart') }}").done(function(data) {
    $(".dropdown-cartlist").html("");
    if (data.length == 0) {
      $(".dropdown-cartlist").html("<li><span class='item'><h4>No items on the cart.</h4></span></li>");
    } else {
      for (var i = 0; i < data.length; i++) {
        var cart_items = "<li>" +
            "<span class='item'>" +
            "<span class='item-left'>" +
            "<img width='50px' height='50px' src='{{ URL::asset('assets/images/uploads/') }}/" + data[i].image + "' alt='' />" +
            "<span class='item-info'>" +
            "<span>" + data[i].product_name + " x " + data[i].quantity + "($" + data[i].pricee + ")</span>" +
            "<span>$" + data[i].total + "</span>" +
            "</span>" +
            "</span>" +
            "<span class='item-right'>" +
            "<button data-id='" + data[i].cart_id + "' class='removeProduct btn btn-xs btn-danger pull-right'>x</button>" +
            "</span>" +
            "</span>" +
            "</li>";
        $(".dropdown-cartlist").append(cart_items);
      };
    }
  });
}
cartlist();
函数cartList(){
$.get(“{{URL::route('myCart')}}”).done(函数(数据){
$(“.dropdownlist”).html(“”);
如果(data.length==0){
$(“.dropdown cartlist”).html(“
  • 购物车上没有项目。
  • ”; }否则{ 对于(变量i=0;i”+ "" + "" + "" + "" + “+数据[i]。产品名称+“x”+数据[i]。数量+”($“+数据[i]。价格+”)+ “$”+数据[i]。总计+”+ "" + "" + "" + “x”+ "" + "" + “”; $(“.dropdown cartlist”).append(购物车项目); }; } }); }
    Parsed
    html
    如果您要求从
    DOM
    中获取值,则会有所帮助。DOM sir@rayondabrear是什么意思?您试图从解析的html中访问值吗?@jamo我可以回答吗?DOM是文档对象模型的缩写。这是一个缩略语,