douhui0975 2014-02-25 15:02 采纳率: 100%
浏览 44
已采纳

jQuery load()在textarea上没有按预期工作

I'm trying to get user content from PHP file with load() function and show it in the textarea which works fine however same process won't work if the user type something in the texarea beforehand. Anyone has a solution to it?

For example: Type something in textarea and click links which won't change the content of texarea.

Thanks in advance

HTML

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

<script type="text/javascript">
function show_content(display_field, id)
{
    $('#' + display_field).load('data.php?id=' + id);
}
</script>


<a href="#nogo" onClick="show_content('content_area', '1')" title="View">User 1</a>
<a href="#nogo" onClick="show_content('content_area', '2')" title="View">User 2</a>
<a href="#nogo" onClick="show_content('content_area', '3')" title="View">User 3</a>

<textarea id="content_area" rows="10" cols="10"></textarea>

PHP

<?php
$content[1] = 'This belongs to User 1';
$content[2] = 'This belongs to User 2';
$content[3] = 'This belongs to User 3';

echo $content[$_GET['id']];
?>
  • 写回答

2条回答 默认 最新

  • dongzan1970 2014-02-25 15:18
    关注

    Your problem is that load() sets the html rather than the value.

    You could use this:

    function show_content(display_field, id) {
        $.get( 'data.php?id=' + id, function( data ) {
            $('#' + display_field).val(data);
        });
    }
    

    .get()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应