简体   繁体   English

从Ajax加载页面提交Ajax表单

[英]Submitting Ajax Form From Ajax Loaded Page

I have a page which is generated within ajax. 我有一个在ajax中生成的页面。

On that page there is a 2 forms, #form1 and #form2. 在该页面上有#form1和#form2 2种形式。

I have the jquery the code for submitting the form is as follows: 我有jQuery提交表单的代码如下:

jQuery("#form1").on('submit', function(e){

        e.preventDefault();

        var sendurl = '/xxx/xxx/xxx/xxx.php';

        var varable1 = jQuery('#infor1').val();
        var varable2 = jQuery('#infor2').val();
        var varable3 = jQuery('#infor3').val();
        var varable4 = jQuery('#infor4').val();
        var varable5 = jQuery('#infor5').val();
        var varable6 = jQuery('#infor6').val();
        var varable7 = jQuery('#infor7').val();

        jQuery.ajax({url: sendurl + '?varable1=varable1' + 'varable2=' + varable2 + 'varable3=' + varable3 + 'varable4=' + varable4 + 'varable5=' + varable5 + 'varable6=' + varable6 + 'varable7=' + varable7}).done(function(data) {

            if(data === 'fail'){
                jQuery('.error').html('Something went wrong with your form request please try again!').slideDown(500).delay(4000).slideUp(500);
                jQuery('input[type=submit]', jQuery("#submittions")).removeAttr('disabled');
                return false;
            }

            if(data !== 'fail'){
                jQuery('#showresults').slideUp(600);
                jQuery('.show-complete-detail').html(data);
                jQuery('#applicationcomplete').delay(1500).slideDown(400);
            }

        }); 
});

I have also tried the: 我也尝试过:

jQuery().submit();

That does not work either. 那也不行。

I am assuming that because the page is loaded in using ajax that it can not find the form with the id of form1? 我假设因为页面是使用ajax加载的,所以找不到ID为form1的表单?

Has anyone any ideas on where I am going wrong. 有谁对我要去哪里有任何想法。

Thanks :) 谢谢 :)

For dynamically loaded content, use event delegation like so : 对于动态加载的内容,请使用事件委托,如下所示:

// replacing document with top level parent
// something like parent container
jQuery(document).on('submit','#form1', function(e){ ... }

And one more thing, data passed from ajax url like that is bad idea, you should use data properties to send data, see below example : 还有一件事,从ajax url传递数据是个坏主意,您应该使用数据属性发送数据,请参见以下示例:

$.ajax({
     type : 'POST', // or GET
     url : 'your url here',
     data : {
        var1 : variable1
         .......
     },
    .... Another properties...
 }).done(data){...});

Delegate the event to a higher level dom element that wasnt added dynamically. 将事件委托给未动态添加的更高级别的dom元素。

ie)

$("#FormContainer").on("#form1", "submit",function(){
    //   Do Form tuff....
});
$("#FormContainer").on("#form", "submit",function(){
   //   Do Form tuff....
});

bind with document 装订文件

jQuery(document).on('submit','#form1', function(e){


});

use the console.log for sendurl 使用console.log发送sendurl

console.log(sendurl)

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

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