简体   繁体   English

来自ajax的jQuery帖子加载了div

[英]jQuery post from ajax loaded div

Can't seem to figure this one out. 似乎无法弄清楚这一点。 I have a page where contents of one div is loaded via ajax. 我有一个页面,其中一个div的内容通过ajax加载。 That same div has forms in it and after submit it should refresh to show new content. 该div中包含表单,提交后应刷新以显示新内容。 But without reloading the whole page. 但无需重新加载整个页面。 How is that possible? 那怎么可能? I am so lost right now. 我现在迷失了。

Also I don't think it's the best style to loop new form for every row but can't think of anything else now. 而且我也不认为为每一行循环新表单是最好的样式,但是现在什么也没想到。 If someone could help me would be awesome. 如果有人可以帮助我,那就太好了。

display.php display.php

<script>
$(document).ready(function() {    
$("#list").load("list.php");
});
</script>
 .................
 other stuff
 .................

  <div id="list"></div>

list.php list.php

<script>
$(document).ready(function() {
$( ".add" ).click(function(event){
 event.preventDefault();
 $.ajax({type:"POST", url:"ajax_post.php",
 data:$("#"+id).serialize(), cache:false, timeout:10000 });

  location.reload();

   }); });
  </script>

 .................
 other stuff
 .................
  <?php
   foreach($list as $l): ?>
  <form id="<?php echo $l->id;?>">
  <input type="text" value="<?php echo $l->pd;?>" name="pd">
  <input type="text" value="<?php echo $l->sf;?>" name="sf">
  <input type="submit" value="<?php echo $l->status;?>" class="add">
   </form>
   <?php
   endforeach; ?>

ajax_post.php is just insert query. ajax_post.php只是插入查询。

I am not sure to understand your requirements. 我不确定您的要求。 But way don't you try changing your script code with something like: 但是,您不要尝试通过以下方式更改脚本代码:

<script>
$(document).ready(function() {
  $( ".add" ).click(function(event) {
    event.preventDefault();
    $.ajax( {type:"POST", url:"ajax_post.php",
      data: $("#"+id).serialize(), cache:false, timeout:10000 
      success: function() {
        $("#list").empty()
        $("#list").load("list.php");
      }});
});});
</script>

The intention is to clean the #list element and refill it once the ajax call succeeded. 目的是清理#list元素,并在ajax调用成功后重新填充它。

First, put your form in a separate php file so that we can call it using AJAX, eg forms.php. 首先,将您的表单放在单独的php文件中,以便我们可以使用AJAX对其进行调用,例如Forms.php。

When the page loads, you call list.php using $.load() 页面加载时,使用$.load()调用list.php $.load()

$('#list').load('list.php')

From my understanding, the list will contain forms, and when the form is submitted, you want the form to reload again without having page refresh. 据我了解,该列表将包含表单,并且在提交表单时,您希望表单重新加载而无需刷新页面。 So this is my approach. 这就是我的方法。

$('#list').on('submit', '#formIDfromPHP', function(){
    var data = $(this).serializeArray()
    $.post('ajax_post.php', data, function(result,status){
        console.log(result)
    })
    // Dynamically load it again without refresh.
    $('#list').load('list.php')

    // Or maybe you want to load the form only, hence the external forms.php
    // $('#list').load('form.php')

    // Prevents default form submission behaviour.
    return false;
})

Hope this helps. 希望这可以帮助。 Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM