php - 在 JQuery/Javascript 中使用值的问题

标签 php javascript jquery ajax

我有一个来自 Mysql 的 PHP 填充表,我正在使用 JQuery 来监听是否单击了按钮,如果单击它,它将获取有关他们单击的关联名称的注释。一切都很好,只有一个问题。有时,当您单击它并打开对话框(JQuery UI)窗口时,文本区域中什么也没有。如果您要再次单击它,它将重新弹出。所以有时看起来,也许值(value)被抛弃了?我不确定,可以用手。

代码:

$(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);

    }

如果您还需要什么,请告诉我!

更新:

基本布局是

<div>
   <div>
     other stuff...

     the table
   </div>
</div>

最佳答案

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

最好的方法是等待两个 ajax 调用完成,然后继续。您可以使用 ajax 调用返回的 promises/jqXHR 对象来做到这一点,请参阅 this section of the manual .

您的代码看起来像(您必须对其进行测试...):

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)
    });
}

您正在调用的函数应该只返回 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()
    }
  });
}

关于php - 在 JQuery/Javascript 中使用值的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14841907/

相关文章:

php - 如何覆盖 yii 中模块模型的方法?

php - 在 URL 中使用 & 符号

php - 电子邮件验证后将数据发送到数据库

Javascript html函数输入按钮输出

javascript - Arshaw fullcalendar 在月 View 的每个单元格中添加自定义 html

javascript - 在 Firefox 插件中打开包含表单的窗口

jquery - 使用 CSS nth-child 选择单个表格单元格

PHP函数问题

jquery - 检查文本框是否有空值

javascript - stopPropagation in scroll() - 方法不会阻止外部 div 滚动