doureng5668 2015-02-07 15:54
浏览 70
已采纳

使用jQuery刷新<DIV>并获得新结果

I use an HTML form where I ask the user to select some data in a scroll menu. With jQuery and PhP I handle this data, then I display another scroll menu, depending of the item chosen in the first one. The second scroll menu is entirely echoed by a php function.

My second scroll menu is located in a DIV, named "camp". My jQuery code ends like this :

 $('.camp').append(data);

The problem is that each time I select something in the first scroll menu, another second menu displays again. The program appends the result, which is normal. But the results "match", that is to say that the results of the extra menu are consistent with the last selection in the first menu.

So I tried to replace the old data by the new, and I changed my code :

$('.camp').replaceWith(data);

Now it's ok, every time I change something in the first menu, there no appearance of an extra second menu. But now my problem is that the data in the second scroll menu still refer to my first selection.

For instance, if I select the item "1", a second menu displays "1". If now I change my first item to "2", my second scroll menu still displays "1". In other words, it is not refreshed.

Here is a piece of my HTML :

<?php
include 'functions.php';
propose_choix_BDD();
?>
<BR>
<div class="camp"></div>
<BR><BR><BR>

propose_choix_BDD() displays a scroll menu. To put it in a nutshell, I'd like to fill the "camp" div with the result selected in the first scroll menu. If I write this : ...

$('.camp').replaceWith(data);

...

The first time I select something, it displays properly. But the next times, the value displays doesn't change anymore. Though, "data" changes, as I test it in my success: jQuery script.

How could I slove this ?

Thanks

  • 写回答

1条回答 默认 最新

  • duankanjian4642 2015-02-07 16:25
    关注

    You should use .html(), not .replaceWith(). .html() replaces the contents of the DIV with new contents, .replaceWith() removes the entire DIV and puts the new contents in its place. When you use .replaceWith(), there's no longer a DIV with class="camp", so future uses will find nothing to replace.

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

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题