为什么我的全局变量为null?的JavaScript / jeditable

时间:2011-08-31 20:44:42

标签: javascript jquery ruby-on-rails jeditable

这是一个非常奇怪的。我会让代码进行讨论。

var update_address = location.href+"/update.json"
var currentPageId = null;

$(document).ready(function() {

    $('.editable-td').click(function(){
        currentPageId = this.id;
        console.log(currentPageId);
    });

    $('.editable-td').editable(update_address, {
        submitdata:{
            pageID: currentPageId //This is the part to watch
        },

        type      : 'textarea',
        cancel    : 'Cancel',
        submit    : 'OK',
        tooltip   : 'Click to edit...'
    });
});

第一个函数将变量定义为currentPageId,并将其打印到控制台。但是当我尝试将其传递给jeditable函数时,它会显示为null。任何想法?

1 个答案:

答案 0 :(得分:0)

之前我使用过这个插件。使用函数而不是url:

$('.editable-td').editable(function(value, obj){
        var self = this; //this is the `.editable-td` that was clicked
        console.log(self, obj, value);
        //now use jQuery's $.post with the url and the result
        $.post(update_address, {pageID: self.id, submit_value: value}, 
               function(data){
                    //do something with data
               });
        return value;
},{
    type      : 'textarea',
    cancel    : 'Cancel',
    submit    : 'OK',
    tooltip   : 'Click to edit...'
});