weixin_33725272 2017-02-08 13:50 采纳率: 0%
浏览 24

jQuery + PHP。 请求/响应

I would search for the solution, but I don't know what exactly do I have to search.

The task is to grab texts with ID's (#ftext_1,..._2,..._3,..._4) in html file and send them to php file. After some manipulation with texts in php file I have to insert them back into their ID's in html file. Here is the code:

var text_1_Replace = $('#ftext_1').text();
var text_2_Replace = $('#ftext_2').text();
var text_3_Replace = $('#ftext_3').text();
var text_4_Replace = $('#ftext_4').text();

$('#ID').on('click', function(){
var text= {
        ftext_1: text_1_Replace,
        ftext_2: text_2_Replace,
        ftext_3: text_3_Replace,
        ftext_4: text_4_Replace
    }
     var targetFile = 'ajax/file.php';

    $.ajax({
        method: 'post',
        url: targetFile ,
        data: JSON.stringify(text),
        contentType: 'application/JSON'
    }).done(function(data) {
       console.log(data);
    });
});

How do I edit .done function to place new texts in their old ID's(#ftext_1,..._2,..._3,..._4)? The variable with texts array is $result.

so the answer is :

}).done(function(data) {
        var text = JSON.parse(data);

        var text1 = text.ftext_1;
        var text2 = text.ftext_2;
        var text3 = text.ftext_3;
        var text4 = text.ftext_4;
        $('#ftext_1').text(text1);
        $('#ftext_2').text(text2);
        $('#ftext_3').text(text3);
        $('#ftext_4').text(text4);

So, the last update for the topic: The real and nice answer is:

.done(function(data) {
        var text = JSON.parse(data);
        $.each(text, function(i, val){
            $("#" + i).text(val);
        });

This code is the solution to my question in this topic. Thank you all, who responded!

  • 写回答

2条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 Android URL如何转成视频/音频,可行吗?
      • ¥20 SQL数据查询,子查询
      • ¥15 c++字符串分割问题
      • ¥15 关于#sql#的问题:没有用命令关闭cdc,手动把系统表开启的cdc右键删除了
      • ¥15 vue+uniapp
      • ¥15 android freedom
      • ¥15 使用自定义的类型代替内置类型可行吗
      • ¥15 关于STM32的SPI和ENDAT接口编码器通信的问题
      • ¥15 关于#pdfbox#生成的PDF文件正常,转图片中文乱码的问题,如何解决?
      • ¥15 ADS中有关DAC控件的使用问题