0

我是 jQuery 新手,无法在 Firebug 中调试这个 ajax 调用:

这是我的ajax调用:

var styndx = $('#studylist option:selected').val();
var studyname = $('#edit_field').val();


$.post("saveStudyName.php", {'type': 'update', 'studyname':studyname, 'styndx':styndx},
    function(resultmsg) {
    $('#edit_field').val('');
    $('#savebtn').attr('disabled',true);
    refresh_studynames();
});

这是函数 refresh_studynames:

function refresh_studynames()
{
  $.ajax({                                      
     url: 'getStudyNames.php',                  
     data: "",                                                             
     dataType: 'json',               
          error: function() {
            alert('Refresh of study names failed.');
          },
     success: function(data)
     {
        $data.each(data, function(val, sname) {
        $('#studylist').append( $('<option></option>').val(val).html(sname) )
      });
     } 
  });
}

最后,这是 php 脚本 getStudyNames.php($dbname、$dbconnect、$hostname 都已填充,并且 $dbconnect 有效;后端数据库是 Postgres,而pg_fetch_all是 PHP 中的 Postgres 函数,它以数组形式返回结果):

$dbconnect = pg_pconnect("host=".$hostname." user=".$dbuser." dbname=".$dbname);    
    if (!$dbconnect)    {
        showerror(0,"Failed to connect to database",'saveStudyName',30,"username=".$dbuser.", dbname=".$dbname);
        exit;
    }

    $sql = "SELECT ST.studyindex,ST.studyabrv AS studyname
            FROM ibg_studies ST
            ORDER BY studyname";


    $fetchresult = pg_exec($dbconnect, $sql);
    if ($fetchresult) {
        $array = pg_fetch_all($fetchresult);
        echo json_encode($array);
    } else {
        $msg = "Failure! SQL="+$sql;
        echo $msg;
    }

非常感谢任何帮助....

4

1 回答 1

1

线

$('#studylist').append( $('<option></option>').val(val).html(sname) );

看起来不对。

我不太确定,但您可以尝试:

var $studylist = $('#studylist').empty();
$data.each(data, function(i, record) {
    $studylist.append( $('<option/>').html(record.sname) );
});
于 2012-12-07T02:09:47.990 回答