如何在表单字段中填充AJAX POST值


$(document).ready(function() {
  $('#content-area').mouseup(function() {
   var selection = getSelected();
   if(selection && (selection = new String(selection).replace(/^s+|s+$/g,''))){
         $.ajax({
         type: 'POST',
         url : 'check.php',
         data: 'selection=' + encodeURI(selection),
   });
   }
  });

现在如何访问已发布的"数据"并填写HTML表单的文本字段。我在Ajax帖子期间没有单击一个按钮,我想在不重新加载整个页面的情况下进行操作。任何帮助都非常感谢..

您需要在AJAX调用中定义A success处理程序。该处理程序将采用三个参数,第一个参数是您从服务器收到的数据...

$(document).ready(function() {
  $('#content-area').mouseup(function() {
   var selection = getSelected();
   if(selection && (selection = new String(selection).replace(/^s+|s+$/g,''))){
         $.ajax({
         type: 'POST',
         url : 'check.php',
         data: 'selection=' + encodeURI(selection),
         success: function(data){
            alert(data.d);
            $('text_input_selector').val(data.d);
        }
   });
   }
  });

请注意,text_input_selector必须是文本输入的有效选择

尝试以下代码样本:

$(document).ready(function () {
    $('#content-area').mouseup(function () {
        var selection = getSelected();
        if (selection && (selection = new String(selection).replace(/^s+|s+$/g, ''))) {
            $.ajax({
                type: 'POST',
                url: 'check.php',
                data: 'selection=' + encodeURI(selection),
                beforeSend: function () {
                    //do something
                }
                success: function (response) {
                    $("TEXTFIELD_ID").val(response);
                }
            });
        }
    });

写成功回调处理程序。喜欢

 $.ajax({
         type: 'POST',
         url : 'check.php',
         data: 'selection=' + encodeURI(selection),
        contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                var returnedvalue = result.d;
                debugger;
            },

最新更新