AjaxForm函数第二次不起作用

时间:2018-05-31 20:47:30

标签: jquery

我正在尝试使用AjaxForm来更新数据。在第一次调用时,它工作得很好,并通过id更新我的html文本。

            <script type="text/javascript">
            $( document ).ready(function() {
                $('.entry_form').ajaxForm(function(data) { 
                        $("#results").html(data);
                });
                $('.submitclosest').on('click', function(e) {
                    e.preventDefault();
                    $(this).closest('form').submit();
                });
            });
        </script>

在我的提取文件中,我得到了这一行:

<script>$('#sales_<?=$tid;?>_<?=$position_json;?>').html('<?php ShowSlot($tid, $position_json);?>');</script>

我在函数文件中得到了这个:

    function ShowSlot($tid, $position_json)
{
?><form action="fetch/add_entry.php" method="post" class="entry_form"><input type="hidden" name="tid" value="<?=$tid;?>" /><input type="hidden" name="position" value="<?=$position_json;?>" /><a href="#" class="submitclosest"><i class="fas fa-user-plus"></i></a></form><?php   
}

知道为什么它只能在第一次刷新时起作用吗?

2 个答案:

答案 0 :(得分:0)

乍一看,似乎你只是在

上调用AJAX函数

$(document).ready

这意味着只有在页面首次加载或刷新时才会调用它。如果要在页面加载后更新表单,则必须通过计时器或单击事件或类似事件再次调用AJAX函数。

编辑:实施示例

假设submitclosest是用户在提交时点击的元素,我只是这样做:

$( document ).ready(function functionName() {
            $('.entry_form').ajaxForm(function(data) { 
                    $("#results").html(data);
            });
            $('.submitclosest').on('click', function(e) {
                e.preventDefault();
                $(this).closest('form').submit();
                functionName;
            });
        });

答案 1 :(得分:-1)

function convertFormToAjaxSubmit () {
  //initialize the entry_form in the results to submit with ajax
  //this only effects the elements that exist in the DOM at that point in time
  $('#results .entry_form').ajaxForm(function(data) {
    //the following line replaces the elements that were previously initialized with
    //different elements
    $("#results").html(data);
    //call the method again to bind on the new form created in the results
    convertFormToAjaxSubmit();
  });
}

convertFormToAjaxSubmit();

相关问题