weixin_33713350 2018-11-24 15:50 采纳率: 0%
浏览 307

使用Ajax获取<td>的值

In my Checkout page, i have table which is display products where customer want to buy and than i want get value of each <td> and save it into database, but i dont know what did i wrong, i also get class to each <td>, but it couldn't get value of <td> and than when i check DB i can see row is inserted but contains null value. Can anyone please help me or point me into the right direction :)

This is my code so far:

HTML:

<div class="container">
    <h1>Shopping Checkout</h1>
    <table class="table table-hover">
        <thead class="thead-inverse">
            <tr>
                <th>Qty</th>
                <th>Item Name</th>
                <th>Cost</th>
                <th class="text-xs-right">Subtotal</th>
            </tr>
        </thead>
        <tbody id="output"> </tbody>
    </table>
    <input type="submit" class="btn btn-primary btn-block placeorder" value="PlaceOrder">
</div>

JS:

$(document).ready(function () {

        //Display Products in Checkout list
        function myfunction() {
            $.ajax({
                type: "GET",
                url: "http://localhost:xxx/api/Values/ProdukterudID" + params,
                dataType: "json",
                success: function (values) {
                    var total = 0;
                    for (var i = 0; i < values.length; i++) {
                        value = values[i]
                        if (value != null) {
                            var stotal = localStorage.getItem("prod_" + value.ProductID) * value.ProductPrice;
                            total += stotal;

                           //here i defined <td> with class
                            holderHTML += '<tr><td class="Qty">' + localStorage.getItem("prod_" + value.ProductID) + '</td><td class="ProductName"> ' + value.ProductName + '</td><td> ' + formatMoney(value.ProductPrice) + ' </td><td class="Subtotal" class="text-xs-right"> ' + formatMoney(stotal) + '</td></tr>'
                        }

                    }

                    $('#output').html(holderHTML);
                },
            })
        }

        myfunction();

       //Button click save into database
            $(".placeorder").click(function (e) {

                e.preventDefault();

                var tr = $(this).closest("tr");
                var model = {
                    Qty: tr.find(".Qty").text(),
                    Subtotal: tr.find(".Subtotal").text(),
                    ProductName: tr.find(".ProductName").text(),
                }
                $.ajax({
                    type: 'POST',
                    url: "http://localhost:xxx/api/Values/PlaceOrder",
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    data: {
                        Qty: model.Qty,
                        Subtotal: model.Subtotal,
                        ProductName: model.ProductName,
                        Email: localStorage.getItem("Email")
                    },
                    success: function (run) {

                        if (run)
                        {
                            console.log("Ok");
                        }

                        else {
                            console.log("error");
                        }                   
                    }

                });
            });
     });

Controller:

[Route("api/Values/PlaceOrder")]
[HttpPost]
public IHttpActionResult PlaceOrder() {

    DateTime CreatedAt = DateTime.Today;
    CustomerOrdersVM model = new CustomerOrdersVM();

    var SaveOrdre = new CustomerOrders
    {
        Qty = model.Qty,
        Email = model.Email,
        ProductName = model.ProductName,
        Subtotal = model.Subtotal,
        CreatedAt = CreatedAt,
    };

    db.CustomerOrders.Add(SaveOrdre);
    db.SaveChanges();

    return Ok(model);
}
  • 写回答

1条回答 默认 最新

  • ?Briella 2018-11-24 19:39
    关注

    Is the placeholder button in the same TR as the row that contains the data being processed? It didn't appear so in the dynamic markup sample you are adding.

    $(".placeorder").click(function (e) {
       e.preventDefault();
       var tr = $(this).closest("tr"); // button in same TR as data cell?
    
        var model = {  Qty: tr.find(".Qty").text(), .. } // same row as button?
    
    评论

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab