动态加载表单和提交脚本

发布于 2024-11-24 18:36:23 字数 2180 浏览 1 评论 0原文

我正在将表单动态加载到 div 中。使用 JQuery.post 发布表单时,应显示结果而无需重新加载整个页面。但响应中收到的内容会加载到整个浏览器中,因此主页不再显示菜单、徽标等。有很多 php 代码生成内容,这就是我认为现在可能感兴趣的内容:

<div id='subscribe_page'>
<div id='sub' class='subscribe'>
<form id='subscribe_now' action='subscription.php' name='subscribe' method='post'>
    <p class='formlabel'>Förnamn</p> <input type='text' id='first_name' name='first_name'/><br/>
    <p class='formlabel'>Efternamn</p> <input type='text' id='surname' name='surname'/> <br/>
    <p class='formlabel'>E-mail</p> <input type='text' id='email1' name='email1'/><br/>
    <p class='formlabel'>Repeat e-mail</p> <input type='text' id='email2' name='email2'/> <br/>
    <input class='inputsubmit' type='submit' value='Subscribe'/>
</form>
</div>
</div>
<script language='javascript' type='text/javascript' src='jquery-1.6.2.min.js'></script>
 <script language='javascript' type='text/javascript'>

  $(document).ready(function() {
   $('#subscribe_now').submit(function() {
   regExp = /^[^@]+@[^@]+$/;

   if(($('#email1').val().search(regExp) == -1) || !isEqual($('#email1').val(), $('#email2').val())) 
   { 
       alert('Incorrect entered email addresses. They must be valid e-mail addresses and equal.');
      return false; 
   } else{
    alert('Posting data' + $('#subscribe_now').serialize());
    $.post('subscription.php', $('#subscribe_now').serialize(), function(data) {
        $('#subscribe_page').html(data);
        alert('Posted data');
    });
    return true;
   }     

});

});     
</script>"

上面的所有内容都加载到属于主页的 div 中。发布后,我希望响应中的数据显示在 div subscribe_page 中。但事实并非如此。

请注意,$.post 内的警报不会被触发。不过,前一行的警报被触发,结果如下:

first_name=sertdg&surname=5tghf&email1=niklas%40iandapp.com&email2=niklas%40iandapp.com

我也尝试指定应该在行为上没有差异的情况下返回的内容:

$.post('subscription.php', $('#subscribe_now').serialize(), function(data) {
        $('#subscribe_page').html(data);
        alert('Posted data');
}, 'html');

问题可能是什么以及如何解决它?

I am loading a form dynamically into a div. When posting the form using JQuery.post, the result should be presented without the entire page to be reloaded. But instead the content received in the response is loaded into the entire browser, hence the main page is not present with menu, logos etc anymore. There's a lot of php code generating the content, here's just what I think may be of interest now:

<div id='subscribe_page'>
<div id='sub' class='subscribe'>
<form id='subscribe_now' action='subscription.php' name='subscribe' method='post'>
    <p class='formlabel'>Förnamn</p> <input type='text' id='first_name' name='first_name'/><br/>
    <p class='formlabel'>Efternamn</p> <input type='text' id='surname' name='surname'/> <br/>
    <p class='formlabel'>E-mail</p> <input type='text' id='email1' name='email1'/><br/>
    <p class='formlabel'>Repeat e-mail</p> <input type='text' id='email2' name='email2'/> <br/>
    <input class='inputsubmit' type='submit' value='Subscribe'/>
</form>
</div>
</div>
<script language='javascript' type='text/javascript' src='jquery-1.6.2.min.js'></script>
 <script language='javascript' type='text/javascript'>

  $(document).ready(function() {
   $('#subscribe_now').submit(function() {
   regExp = /^[^@]+@[^@]+$/;

   if(($('#email1').val().search(regExp) == -1) || !isEqual($('#email1').val(), $('#email2').val())) 
   { 
       alert('Incorrect entered email addresses. They must be valid e-mail addresses and equal.');
      return false; 
   } else{
    alert('Posting data' + $('#subscribe_now').serialize());
    $.post('subscription.php', $('#subscribe_now').serialize(), function(data) {
        $('#subscribe_page').html(data);
        alert('Posted data');
    });
    return true;
   }     

});

});     
</script>"

Everything above is loaded into a div belonging to the main page. After posting, I want the data in the response to be presented in div subscribe_page. But it is not.

Please note that the alert inside the $.post isn't triggered. The alert the row before is triggered though, with result like this:

first_name=sertdg&surname=5tghf&email1=niklas%40iandapp.com&email2=niklas%40iandapp.com.

I did also try to specify the content that should be returned with no difference in behaviour:

$.post('subscription.php', $('#subscribe_now').serialize(), function(data) {
        $('#subscribe_page').html(data);
        alert('Posted data');
}, 'html');

What could the problem be and how to solve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟花肆意 2024-12-01 18:36:23

好吧,我不太明白,但如果你这样做了:

 $('#subscribe_page').html(data); 

你实际上删除了该 div 中的所有内容,并将其替换为你的响应。也许你应该这样做:

 $('#subscribe_page').after(data);

 $('#subscribe_page').append(data);

编辑 - 因为问题出在你的 $.post 中,我认为你不应该使用 serialize () 因为该函数与 $.get() 一起使用。您应该查看 param()serializeArray()

Well i don't understand exactly, but if you do:

 $('#subscribe_page').html(data); 

you actually erase whatever you have inside that div and replace it with your response. Maybe you should do:

 $('#subscribe_page').after(data);

or

 $('#subscribe_page').append(data);

EDIT - since the problem is in your $.post, i think that you should not use serialize() as that function is used with $.get(). you should look into param() or serializeArray()

黒涩兲箜 2024-12-01 18:36:23

由于表单是动态加载的,我需要像这样使用“live”将新内容加载到div中:

    $("#registerform").live("submit", function(e){

            //Prevent the form from submitting normally
            e.preventDefault();

            $.post("registeraccount.php",$(this).serialize(),function(msg){

                $("#adminarea").html(msg);                          

        });
    });

据我了解,“live”必须用于动态内容。

Since the form is loaded dynamically, I need to load the new content into the div like this, using "live":

    $("#registerform").live("submit", function(e){

            //Prevent the form from submitting normally
            e.preventDefault();

            $.post("registeraccount.php",$(this).serialize(),function(msg){

                $("#adminarea").html(msg);                          

        });
    });

As I understand it, "live" must be used for dynamic content.

落墨 2024-12-01 18:36:23

您可能只需要更改按钮的类型,以便 HTML 不会将其视为提交按钮。如果它将其视为提交按钮,它将以正常方式提交表单,重新加载整个页面或加载下一页,具体取决于设置方式。只需将按钮设置为非提交类型,您的问题就可以解决。

You may just need to change the type of button you have so that HTML doesn't see it as a submit button. If it sees it as a submit button, it will submit the form in the normal way, reloading the entire page or loading the next page depending on how things are set up. Just set the button to be of a non-submit type, and your problem might be solved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文