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

使用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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题