-1

関数は、Json を返すデータベース呼び出しをトリガーします。この CID は、新しく追加された DOM 要素をスタンプするために使用されます。

ajax 成功ブロック内に存在する「cid」の値を取得/返す必要があります。これは属性の更新に使用されます。

エラー cid が定義されていません

//Save New Task
$("body").on('click','.icon-facetime-video',function(event){
  var new_entry = $(this).prev().val();
  var thebid = $(this).parents('tbody').prev().find('th').attr('id').split('_')[1];
  $(this).parent().addClass('tk').html('<i class="icon-eye-open"></i> '+new_entry);
  //CALL FUNCTION
  var cid = update_entry('new_tk',new_entry,thebid); //value of cid from update_entry
  $(this).parent().attr('id','cid_'+cid);
});

function update_entry(typeofupdate,new_entry,thebid){

  switch(typeofupdate){
    case 'new_tk': var action = 'new_tk';
    break;
  }

  $.ajax({
        type:'post',
        url: 'posts_in.php',
        dataType: "json",
        data: {cid : thebid, action: action, content: new_entry},
        success: function(data){
            var action = data[0],cid= data[1];
            console.dir(data);
        }
    });

  return cid;  
}
4

1 に答える 1

2

AJAX は非同期で行われるため ( aAJAX では )、AJAX 呼び出しが完了する前に関数が返されます。コールバック関数を に渡し、update_entryAJAX 成功ハンドラで実行する必要があります。

var self = this;

update_entry('new_tk', new_entry, thebid, function(cid) {
    $(self).parent().attr('id', 'cid_' + cid);
});

function update_entry(typeofupdate, new_entry, thebid, callback) {

  switch(typeofupdate){
    case 'new_tk': var action = 'new_tk';
    break;
  }

  $.ajax({
        type:'post',
        url: 'posts_in.php',
        dataType: "json",
        data: {cid : thebid, action: action, content: new_entry},
        success: function(data){
            var action = data[0], cid= data[1];
            callback( cid );
            console.dir(data);
        }
    });
}
于 2012-05-21T18:43:09.287 に答える