在JQuery/Javascript中使用值的问题

Dav*_*iga 6 javascript php ajax jquery

嘿伙计们,我有一个来自Mysql的PHP填充表,我正在使用JQuery来监听是否点击了一个按钮,如果点击它,它将获取他们点击的相关名称的注释.这一切都很精彩,只有一个问题.有时当您单击它并打开对话框(JQuery UI)窗口时,文本区域中没有任何内容.如果要再次单击它,它将弹回.所以看起来有时候,价值可能会被抛弃?我不确定并且可以用一只手.

码:

$(document).ready(function () {
    $(".NotesAccessor").click(function () {
        notes_name = $(this).parent().parent().find(".user_table");
      run();
    });

});
function run(){
    var url = '/pcg/popups/grabnotes.php';

    showUrlInDialog(url);
    sendUserfNotes();


}
    function showUrlInDialog(url)
    {
      var tag = $("#dialog-container");
      $.ajax({
        url: url,
        success: function(data) {
          tag.html(data).dialog
          ({
              width: '100%',
                modal: true
          }).dialog('open');
        }
      });
    }
    function sendUserfNotes()
    {

        $.ajax({
        type: "POST",
        dataType: "json",
        url: '/pcg/popups/getNotes.php',
        data:
        {
            'nameNotes': notes_name.text()

        },
        success: function(response) {
            $('#notes_msg').text(response.the_notes)
        }
    });

    }
    function getNewnotes(){
        new_notes = $('#notes_msg').val();
        update(new_notes);  
    }
    // if user updates notes
    function update(new_notes)
    {

            $.ajax({
        type: "POST",
        //dataType: "json",
        url: '/pcg/popups/updateNotes.php',
        data:
        {
            'nameNotes': notes_name.text(),
            'newNotes': new_notes   
        },
        success: function(response) {
            alert("Notes Updated.");
            var i;
             $("#dialog-container").effect( 'fade', 500 );

            i = setInterval(function(){
             $("#dialog-container").dialog( 'close' );
            clearInterval(i);
            }, 500);

            }
    });

    }
    /******is user closes notes ******/
    function closeNotes()
    {
        var i;
         $("#dialog-container").effect( 'fade', 500 );

        i = setInterval(function(){
         $("#dialog-container").dialog( 'close' );
        clearInterval(i);
        }, 500);

    }
Run Code Online (Sandbox Code Playgroud)

需要帮助请叫我!

更新:

基本布局是

<div>
   <div>
     other stuff...

     the table
   </div>
</div>
Run Code Online (Sandbox Code Playgroud)

jer*_*oen 1

假设#notes_msg位于#dialog-container,您必须确保操作以正确的顺序发生。

最好的方法是等待两个 ajax 调用完成然后继续。您可以使用 ajax 调用返回的 Promise/jqXHR 对象来做到这一点,请参阅手册的这一部分。

你的代码看起来像这样(你必须测试它......):

function run(){
    var url = '/pcg/popups/grabnotes.php';
    var tag = $("#dialog-container");

    var promise1 = showUrlInDialog(url);
    var promise2 = sendUserfNotes();

    $.when(promise1, promise2).done(function(data1, data2) {
      // do something with the data returned from both functions:
      // check to see what data1 and data2 contain, possibly the content is found
      // in data1[2].responseText and data2[2].responseText

      // stuff from first ajax call
      tag.html(data1).dialog({
          width: '100%',
          modal: true
        }).dialog('open');

      // stuff from second ajax call, will not fail because we just added the correct html
       $('#notes_msg').text(data2.the_notes)
    });
}
Run Code Online (Sandbox Code Playgroud)

您正在调用的函数应该只返回 ajax 调用的结果,而不执行任何其他操作:

function showUrlInDialog(url)
{
  return $.ajax({
    url: url
  });
}
function sendUserfNotes()
{
  return $.ajax({
    type: "POST",
    dataType: "json",
    url: '/pcg/popups/getNotes.php',
    data: {
        'nameNotes': notes_name.text()
    }
  });
}
Run Code Online (Sandbox Code Playgroud)