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.

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

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行