dongwenxiu5200 2012-04-11 09:00
浏览 47
已采纳

通过jquery ajax将文本区域中的内容加载到div中

I need to load content from a text area into a div via jquery ajax. The content initally loads into a div via jquery. When the button is pressed, a click function inserts the content into a database via ajax and then reloads the content back into the div. It works but seems to be a bit sporadic. My question is, is there a better way to do this.

I am basically trying to submit a status into a timeline without refreshing the page

<script>
    $.ajaxSetup ({ cache: false });     
    var ajax_load = "<div align='center'><img src='img/ajax-loader.gif' alt='loading...' /></div>";
    var loadUrl = "ajax/ajax.php";
    var loadMe = "functions/status.php";
    $("#result").html(ajax_load).load(loadUrl);

    $("#share-but").click(function() {
        var status = $("textarea#status").val(); 
        $.post(
            loadMe,
            { status: status, user_id: '<?php echo "10" ?>' },
            "html"
        );
        $("#result").html(ajax_load).load(loadUrl);
        $("textarea").val('Share an update');
    });
</script>
  • 写回答

1条回答 默认 最新

  • dou5454954610 2012-04-11 09:17
    关注

    I am guessing you should be doing your update in the callback function that jquery executes when the server responds to your post request

    thus your code should be changed to

    <script>
        $.ajaxSetup ({ cache: false });     
        var ajax_load = "<div align='center'><img src='img/ajax-loader.gif' alt='loading...' /></div>";
        var loadUrl = "ajax/ajax.php";
        var loadMe = "functions/status.php";
        $("#result").html(ajax_load).load(loadUrl);
    
        $("#share-but").click(function() {
            var status = $("#status").val(); 
            $.post(
                loadMe,
                { status: status, user_id: '<?php echo "10" ?>' },
                function(htmlData)
                {
                   $("#result").html(htmlData);
                   $("textarea").val('Share an update');
                },
                "html"
            );
    
        });
    </script>
    

    Thus here your posts response is html data that is set in the result container

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测