从X-editable获取文本框值

时间:2015-01-22 07:32:06

标签: ajax django x-editable

如何获取文本框字段的值并在data内以ajaxOptions的形式发送?我测试了我的视图,它从我的Django视图中成功打印出测试变量。我正在使用X-editable作为Jquery。下面是文本框输入的样子:

http://jsfiddle.net/xBB5x/197/

views.py

def create_post(request):

    print request.POST.get("test", "");
    return HttpResponse(json,content_type="application/json")

HTML

<a id="other1" data-pk="First Name" data-name="test">First Name</a>

AJAX

$(document).ready(function() {

        $('#other1').editable({
            type: 'text',
            pk: 1,
            url: '/create_post/', 
            ajaxOptions: {

                    data: {

                    csrfmiddlewaretoken: '{{ csrf_token }}',
                    test: "hi",
                    },


                },  
            placement: 'top',
            title: 'New Expense',
            success: function(response, newValue) {
                if(response.status == 'error') return response.msg; //ms
            },  

        });

    });

1 个答案:

答案 0 :(得分:0)

而不是ajaxOptions,请使用params。如果我没记错的话,test及其值将通过x-editable包含在您的POST请求中。尝试这样的事情:

<强> HTML

<a id="other1" data-pk="1" data-name="test">First Name</a>

<强> AJAX

$(document).ready(function() {
    $('#other1').editable({
        type: 'text',
        url: '/create_post/', 
        params : function(params) {
            params.csrfmiddlewaretoken = '{{ csrf_token }}';
            return params;
        }, 
        placement: 'top',
        title: 'New Expense',
        success: function(response, newValue) {
            if(response.status == 'error') return response.msg; //ms
        },  

    });
});