【问题标题】:Sending information with ajax from knockout js array从淘汰赛js数组使用ajax发送信息
【发布时间】:2016-03-25 12:45:48
【问题描述】:

我有一张包含此类信息的表格

我正在使用淘汰赛js并将所有数据放在一个数组上,然后像这样放在这个表上。

self.allchildrendata = ko.observableArray();
self.viewAllUserChildrenCard = function () {

            $.ajax({
                type: 'POST',
                url: BASEURL + '/index.php/main/childrenCardInfo',
                contentType: 'application/json; charset=utf-8',
                dataType :'json'
            })
            .done(function(childrencards) {
                self.allchildrendata.removeAll();
                $.each(childrencards, function (index, childrencard) {
                self.allchildrendata.push(childrencard);
                console.log(self.allchildrendata());
                });
            })
            .fail(function(xhr, status, error) {
                alert(status);
            })
            .always(function(data){                 
            });
        };
        self.viewAllUserChildrenCard();

所以接下来我想点击 rebecca 的 add money 按钮并想发送 rebecca 的 orig id,这样我就可以用它在数据库中找到她并添加钱,但我不确定如何发送orig_id,我试过这种方式。

self.giveCashtoChild = function(){
            $.ajax({
                type: 'POST',
                url: BASEURL + '/index.php/main/addUserChildrenCash' + "/" + self.allchildrendata().orig_id ,
                contentType: 'application/json; charset=utf-8'
            })
            .done(function() {

            })
            .fail(function(xhr, status, error) {
                alert(status);
            })
            .always(function(data){                 
            });
        } 

这是我在每个按钮上都有一个数据绑定的 html 代码,因此我可以发送原始 ID。

<tbody data-bind="foreach: allchildrendata">
                    <tr>
                    <td class="text-center"><span data-bind="text : $data.children_name"></span></td> 
                    <td class="text-center"><span data-bind="text : $data.daugther_son"></span></td>
                    <td class="text-center"><span data-bind="text : $data.amount"></span> $</td>
                    <td class="text-center"><a href="#" data-bind="click : $root.giveCashtoChild"><span class=" glyphicon glyphicon-send"></span></a></td>
                    <td class="text-center"><a href="<?php echo base_url(); ?>index.php/main/takeAway"><span class=" glyphicon glyphicon-trash"></span></a></td>
                    </tr>
            </tbody>

所以基本上我需要帮助来确定我点击了哪个家庭成员并将其发送给那个家庭成员 orig_id

【问题讨论】:

    标签: javascript jquery ajax knockout.js


    【解决方案1】:

    每当您使用click 绑定时,淘汰赛都会通过当前绑定的dataevent

    所以在你的 HTML 中:

    <a href="#" data-bind="click : $root.giveCashtoChild">
    

    它用两个参数调用giveCashToChild。因此,您的 giveCashToChild 方法应该接受两个参数,其中第一个参数是要给现金的孩子。

    self.giveCashtoChild = function(data, event) {
      var currentChildId = data.orig_id;
      // the other stuff..
    };
    

    【讨论】:

    • 试过了,没用,我发布了另一个与此类似的问题,我将其修复为 90%,但 ajax 请求没有通过...请查看stackoverflow.com/questions/36221510/…
    • 你也用currentChildId 替换self.allchildrendata().orig_id 吗?
    • 您的问题是关于确定用户点击了哪个家庭成员。你能确认在我的例子中,currentChildId 持有被点击的家庭成员的 id 吗?事情可能仍然会出错,但我确实希望这部分能够正常工作......有任何错误日志吗?
    • 是的,你的答案有效,所以我接受它,但不会将信息发送给控制器。
    • 看看我发的内容。您可以访问您孩子的所有数据。这样每个孩子都有自己的模型。它们是独立的实体。
    猜你喜欢
    • 1970-01-01
    • 2016-07-19
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 2013-02-09
    • 2013-04-10
    相关资源
    最近更新 更多