将之前的可编辑值发送到服务器

时间:2016-04-12 13:18:40

标签: javascript jquery x-editable

我使用https://vitalets.github.io/x-editable/docs.html作为选择元素。除了将新值发送到服务器(即s,p)之外,我还想发送先前的值(即b)。以下脚本最初会这样做,但只会发送原始值(即p),即使它被多次更改并且新的先前值不再是p。

<a href="javascript:void(0)" class="doc-type" data-value="b"></a>

$('.doc-type').editable({
    type: 'select',
    placement: 'right',
    title: 'Document Type',
    source: [{value:'b',text:'Buy'},{value:'s',text:'Sell'},{value:'p',text:'Project'}],
    //params: {task:'saveDocType',controller:'portal',cid:ayb.component.id,CSRF:ayb.CSRF,doc_id:function(){console.log(this);}},
    params: function(params) {
        //originally params contain pk, name and value
        delete(params.name);
        params.task = 'saveDocType';
        params.controller = 'portal';
        params.cid = ayb.component.id;
        params.CSRF = ayb.CSRF;
        params.doc_id=$(this).parent().parent().data('id');
        params.v_old=$(this).data('value');
        return params;
    },
    url: 'index.php',
    pk: function(){return $('#id').val();},
    error: function (response, newValue) {
        //Unlike other validation, save function to return non 200 header.
        return response.responseText;
    },
});

1 个答案:

答案 0 :(得分:0)

我希望有更好的方法,但有一种方法是将newValue保存在成功回调中。

$('.doc-type').editable({
    type: 'select',
    source: [{value:'b',text:'Buy'},{value:'s',text:'Sell'},{value:'p',text:'Project'}],
    params: function(params) {
        //originally params contain pk, name and value
        params.v_old=$(this).data('value');
        return params;
    },
    url: 'index.php',
    pk: function(){return $('#id').val();},
    success: function(response, newValue) {
        $(this).data('value',newValue)
    }
});