简体   繁体   English

遇到ajax提交重新加载问题,匿名函数未执行

[英]Having Issues with ajax submit reloading, the anonymous function is not executing

I spent quite a bit of time looking for this and maybe I'm not approaching this correctly, but I'm trying to .submit and .post after clicking a submit. 我花了很多时间来寻找这个,也许我没有正确地解决这个问题,但是我试图在单击一个提交之后进行.submit和.post。 In other instances I have been able to get the ajax submit to work properly without the refresh, but when I do it in this manner it just doesn't work. 在其他情况下,我无需刷新即可使ajax提交正常工作,但是当我以这种方式进行操作时,它就无法正常工作。 I'm curious to know why. 我很好奇为什么。

Form 形成

<form id="search" action="process.php" method="post">
    Name: <input id="search_text" type="text" name="name" />
    <input type="submit" value="Submit" />
</form>

Ajax 阿贾克斯

$('#search').submit(function() {
    $.post(
      $(this).attr('action'),
      $(this).serialize(),
      function(data){
        $('#page_nav').html(data.html_page_nav);
        $('#results').html(data.table_html);
      },
      "json"
    );
    return false;
});

This works and it will submit without reloading just fine 这有效,它将无需重新加载就可以提交

Below is where I have the problem. 以下是我遇到的问题。

On the php server side I am sending back html that I want to be able to submit, which the initial search will put into the original html page. 在php服务器端,我正在发回我希望能够提交的html,初始搜索会将其放在原始html页面中。

$html_page_nav = "
    <ul>
";
for($i=1; $i <= get_query_pages(get_query_count($query), 50); $i++) {
    $html_page_nav .= "
        <li>
            <form id='page_".$i."' action='process.php' method='post'>
            <input type='hidden' name='action' value='change_page'/>
            <input type='hidden' name='page' value='".$i."'/>
            <input type='submit' value='".$i."'>
            </form>
        </li>
    ";
}   
$html_page_nav .= "
    </ul>
";

I try to do the same thing as above, but the submit does not work properly 我尝试执行与上述相同的操作,但是提交无法正常工作

            $(document).ready(function() {

                $('#page_1').submit(function() {
                    console.log("this will not display");
                    $.post(
                            $(this).attr('action'),
                            $(this).serialize(),
                            function(data){
                            },
                            "json"
                        );
                        return false;
                })
            ...other jquery
            });

This submit will not work properly, the function() will not execute and it will submit like the regular submit and go to the url rather then execute without refreshing the entire page. 此提交将无法正常工作,function()将无法执行,并且将像常规提交一样提交并转到url,然后执行而不刷新整个页面。

Any suggestions or approaches would be much appreciated. 任何建议或方法将不胜感激。

Thanks in advance! 提前致谢!

Delegate the submit action to execute on content which will be loaded in future. 委托执行提交操作以对将来将要加载的内容执行。 By default the normal event handlers attached to the content loaded on DOM but not the one which will gets loaded in future, say through Ajax. 默认情况下,普通事件处理程序附加到DOM上加载的内容,而不是将来将要加载的内容,例如通过Ajax。 You can use jQuery "on" function to delegate the action on content which will load in future. 您可以使用jQuery“ on”功能来委托对将来将要加载的内容的操作。

eg. 例如。

$('body').on('submit', '#page_1', function() {
  // do it here
});

I had a similar problem using ajaxForm n all. 我在使用ajaxForm n all时遇到了类似的问题。 I jus used $("#Form").validate(); 我必须使用$(“#Form”)。validate(); for it and the page doesnt get refresh 为此,页面不会刷新

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

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